title: Windows Service closed when I disconnect one client socket
body: <p>I have a server listened on a socket. This server is a Windows Service.</p>
  
  <p>My problem is: When I disconnect a client <code>socket.Disconnect(false);</code> the service so closed and other clients are closed forcibly or new connections refused. I think that when service kill this client thread, the service not back to main thread.</p>
  
  <p>Paste my code used for service (server functionality). Is correct the management of threads?</p>
  
  <p>I run server with</p>
  
  <pre><code>this.tcpListener = new TcpListener(ipEnd);
  this.listenThread = new Thread(new ThreadStart(ListenForClients));
  this.listenThread.Start();
  
  private void ListenForClients()
  {
    this.tcpListener.Start();
  
    while (true)
    {
      //blocks until a client has connected to the server
      TcpClient client = this.tcpListener.AcceptTcpClient();
  
      //create a thread to handle communication
      //with connected client
      Thread clientThread = new Thread(new ParameterizedThreadStart(HandleClientComm));
      clientThread.Start(client);
    }
  }
  
  private void HandleClientComm(object client)
  {
    TcpClient tcpClient = (TcpClient)client;
    NetworkStream clientStream = tcpClient.GetStream();
  
    byte[] message = new byte[4096];
    int bytesRead;
  
    while (true)
    {
      bytesRead = 0;
  
      try
      {
        //blocks until a client sends a message
        bytesRead = clientStream.Read(message, 0, 4096);
      }
      catch
      {
        //a socket error has occured
        break;
      }
  
      if (bytesRead == 0)
      {
        //the client has disconnected from the server
        break;
      }
  
      //message has successfully been received
      ASCIIEncoding encoder = new ASCIIEncoding();
      System.Diagnostics.Debug.WriteLine(encoder.GetString(message, 0, bytesRead));
    }
  
    tcpClient.Close();
  }
  </code></pre>
  
  <p>Sorry for my bad english and thanks for any suggestion</p>