private void WatchClients() { while (Listen) { Thread.Sleep(new TimeSpan(0, 1, 0)); //check the servers every 1 minute ConnectedClients.AsParallel().ForAll(o => { try { if (o.TcpClient?.Client != null && o.TcpClient.Client.Connected) { /* pear to the documentation on Poll: * When passing SelectMode.SelectRead as a parameter to the Poll method it will return * -either- true if Socket.Listen(Int32) has been called and a connection is pending; * -or- true if data is available for reading; * -or- true if the connection has been closed, reset, or terminated; * otherwise, returns false */ // Detect if client disconnected if (o.TcpClient.Client.Poll(0, SelectMode.SelectRead)) { byte[] buff = new byte[1]; if (o.TcpClient.Client.Receive(buff, SocketFlags.Peek) == 0) { // Client disconnected ConnectedClients.Remove(o); o.TcpClient.Close(); } } } else { ConnectedClients.Remove(o); o.TcpClient?.Close(); } } catch { ConnectedClients.Remove(o); o.TcpClient?.Close(); } }); Thread.Sleep(16); } }