First off, I'm not an expert about TCP and Sockets, though I have had some experiences in the past.
My problems is that on some occasions it detects disconnect where server does not, so on server I have connection left hanging while client reconnects.
If you have a client-server scenario, then one will never ever know about what is the state of the other without explicitly finding out!
Say you open the connection, client is idle for 10 seconds and now suddenly the client loses all power. The server doesn't know that the client lost it's power, the client didn't send a disconnect signal, the client was in an idle state so there is no sending/receiving that cut out midway or timed out. The only way for the server to know what is the state of the client on the 11-th second (for instance) is to query/ping the client. The term Keep-Alive applies here, although I can't think of good wording for it right now...
I'm asking anyone who tried using this method to share experiences and maybe help make it more robust
The state of the problem isn't currently very clear to me. In what specific scenario does the problem exist? When you remove the LAN cable? When you close the client application?
So what, if the connection on the server is left hanging? You have to account for such a very likely possibility. You will need to apply some sort of timeout or keep-alive methodology, inorder to keep the server from hanging on to the connection indefinently and thus consuming and running out of resources. Accounting for all possible scenarios - that is robustness!
As I said, I'm no expert, so I won't be able to tell you exactly which
Socket class properties or methods to apply to your code to help you make it more robust. But I will say this, your code example is missing
Disconnect() / Close() statements, most likely even
using statements. I see no timeouts being applied, surely the
Connect() and Send() methods have some sort timeout mechanism (maybe a property, eg