title: Proper way to stop TcpListener
body: <p>I am currently using TcpListener to address incoming connections, each of which are given a thread for handling the communication and then shutdown that single connection. Code looks as follows:</p>
  
  <pre><code>TcpListener listener = new TcpListener(IPAddress.Any, Port);
  System.Console.WriteLine("Server Initialized, listening for incoming connections");
  listener.Start();
  while (listen)
  {
       // Step 0: Client connection
       TcpClient client = listener.AcceptTcpClient();
       Thread clientThread = new Thread(new ParameterizedThreadStart(HandleConnection));
       clientThread.Start(client.GetStream());
       client.Close();
  }
  </code></pre>
  
  <p>The <code>listen</code> variable is a boolean that is a field on the class. Now, when the program shuts down I want it to stop listening for clients. Setting listen to <code>false</code> will prevent it from taking on more connections, but since <code>AcceptTcpClient</code> is a blocking call, it will at minimum take the next client and THEN exit. Is there any way to force it to simply break out and stop, right then and there? What effect does calling listener.Stop() have while the other blocking call is running?</p>