c# detecting tcp disconnect
TcpClient / NetworkStream does not get notified when the connection is closed. The only option available to you is to catch exceptions when writing to the stream.
A few years back we moved to using sockets instead of tcp client. socket is more usable as compared to tcpclient.
there are a couple of methods that you can use
Poll is one of them
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.poll.aspx
You can also do a check on outcome of Write itself. it gives you the number of bytes actually written.
The Connected property itself only reflects the state at the last operation. Its documentation states "The value of the Connected property reflects the state of the connection as of the most recent operation. If you need to determine the current state of the connection, make a non-blocking, zero-byte Send call. If the call returns successfully or throws a WAEWOULDBLOCK error code (10035), then the socket is still connected; otherwise, the socket is no longer connected."
http://msdn.microsoft.com/en-us/library/system.net.sockets.socket.connected.aspx