title: TcpClient closed immediately when starting new thread
body: <p>I'm working on a desktop application that involves a <code>TcpListener</code>.</p>
  <p>I'm attempting to create the <code>TcpListener</code> and handle clients in a background thread...</p>
  <pre><code>private TcpListener _listener;
  
  public MainWindow()
  {
      InitializeComponent();
      DataContext = _vm;
          
      Thread smtpThread = new Thread(new ThreadStart(StartSmtpLocal));
      smtpThread.Start();
  }
  
  void StartSmtpLocal()
  {
      _listener = new TcpListener(IPAddress.Any, 1025);
      _listener.Start();
  
      while (true)
      {
          Console.WriteLine(&quot;Waiting for connection...&quot;);
          TcpClient client = _listener.AcceptTcpClient();
          Console.WriteLine($&quot;Client status: {client.Connected}&quot;); // This is 'Client status: True'
  
          SMTPServer handler = new SMTPServer();
          handlers.Add(handler); 
          handler.Initialize(client, SmtpMessageComplete);
  
          Thread thread = new Thread(new ThreadStart(handler.Run));
          thread.Start();
      } 
  }
  </code></pre>
  <p>My <code>SMTPServer.Initialize</code> method is simply storing a reference to the <code>TcpClient</code>.</p>
  <pre><code>private TcpClient _client;
  private Action&lt;MimeMessage&gt; _finishAction;
  private MimeMessage _messageRead;
  
  public void Initialize(TcpClient client, Action&lt;MimeMessage&gt; finishAction)
  {
      _client = client;
      _finishAction = finishAction;
  }
  </code></pre>
  <p>And <code>Run</code> would process messages from the client. However immediately upon entering <code>Run</code> in the new thread, the client is disconnected.</p>
  <pre><code>public void Run()
  {
      Write(&quot;220 localhost -- Fake proxy server&quot;);
      Console.WriteLine($&quot;Client status {_client.Connected}&quot;); // This is 'Client status False'
      ...
  }
  </code></pre>
  <p>Any ideas on what is causing the client to disconnect here?</p>
  <p>Thanks!</p>