Hey everyone. I'm making a game with Sockets. Everything works well except for when the client abruptly closes the game. By that, I mean, he presses ALT - F4 to shut down the game, presses the X in the top right, or ends the task or process.

When the client does such a thing, this error occurs with the server application:

Code:
IOException was unhandled

Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.
This occurs in this block of code:

Code:
// lock ensures that no other threads try to use the stream at the same time.   
lock (client.GetStream())
{
   // Finish asynchronous read into readBuffer and get number of bytes read.
   bytesRead = client.GetStream().EndRead(ar);
}
On this line:

Code:
bytesRead = client.GetStream().EndRead(ar);
I've tried sending a disconnect message when the form is closing on the client's side but it does not alleviate the error.

Normally, this wouldn't be such a big problem, but this crashes the server! Such a thing cannot stand. I suppose I could make the game full screen mode, but one can still ALT - F4, end the task or process, or find some other way of closing the game the wrong way. It would be better to just fix the error.

I have been searching on google for a bit now, but I have yet to find any solutions. Hopefully vbforums can be of some help

Thank you