I have developed server listening to multiple clients using asynchronous sockets. I have used the following method for stopping server from listening to clients.
//Button 2 -- To Stop Server
private void button2_Click(object sender, EventArgs e)
{
socket.Shutdown(SocketShutdown.Both);
socket.Disconnect(false);
socket.Close();
socket.Dispose();
}
But problem is that when I restart the server it shows error socket connection in use. So what is the proper way for closing sockets ans stopping server. I need to stop the server as soon as I press button in UI.