title: Terminate a thread and disconnect an indy client
body: <p>In Delphi I have a threaded client that listens for response in a thread.
  Thread is declared like:</p>
  
  <pre><code>TMyThread = class(TThread)
    private
      FClient: TIdTCPClient;
      FStringResponse: string;
    protected
      procedure Execute; override;
      procedure DoSynch;
    public
      constructor Create(AClient: TIdTCPClient);
    end;
  </code></pre>
  
  <p>I connect using the:</p>
  
  <pre><code>  if not IdTCPClient1.Connected then
    begin
      // // Set the Host
      IdTCPClient1.Host := Edit1.Text;
      // // Set the port
      IdTCPClient1.Port := 65535;
      // // Connect
      IdTCPClient1.Connect;
      try
        MyThread := TMyThread.Create(IdTCPClient1);
      except
        IdTCPClient1.Disconnect;
        raise;
      end;
    end;
  </code></pre>
  
  <p>I try to disconnect using:</p>
  
  <pre><code>if MyThread &lt;&gt; nil then
    begin
      MyThread.Terminate;
      // MyThread.WaitFor;
      MyThread.Free;
      MyThread := nil;
      IdTCPClient1.Disconnect;
  end;
  </code></pre>
  
  <p>But this disconnect code throws an exception. What's the proper way to do terminate this thread and to disconnect the client?</p>