 <p><a href="http://msdn.microsoft.com/en-us/library/system.net.sockets.tcpclient.getstream.aspx" rel="nofollow">TcpClient.GetStream()</a> will throw an exception if the socket is not or no longer connected.</p> <p>Wrap the code in a try..catch block:</p> <pre><code>try { NetworkStream networkStream = clientSocket.GetStream(); networkStream.Read(bytesFrom, 0, 312); dataFromClient = Encoding.ASCII.GetString(bytesFrom); string hex = BitConverter.ToString(bytesFrom).Replace("-",""); Console.WriteLine("\n " + hex + "\n_______________()()()______________"); } catch (InvalidOperationException ioex) { // The TcpClient is not connected to a remote host. } catch (ObjectDisposedException odex) { // The TcpClient has been closed. } </code></pre>