When I tried to reestablish a connection to my Socket I get the following error :
Error : Only one usage of each socket address (protocol/network address/port) is normally permitted
SocketErrorCode : System.Net.Sockets.SocketError.AddressAlreadyInUse
Each time I want to reconnect, I first call the Disconnect() method. But even if my socket is disconnected I have an error when I tried to reconnect with the Connect() method.
public SocketError Connect()
{
//Open socket connection
SocketAsyncEventArgs connectArgs = new SocketAsyncEventArgs();
connectArgs.UserToken = _socket;
connectArgs.RemoteEndPoint = _hostEndPoint;
connectArgs.Completed += OnConnect;
_socket.ConnectAsync(connectArgs); //ERROR : Only one usage of each socket address...
_autoConnectEvent.WaitOne();
if (connectArgs.SocketError != SocketError.Success)
{
throw new SocketException((Int32)connectArgs.SocketError);
}
return connectArgs.SocketError;
}
public void Disconnect()
{
_socket.Shutdown(SocketShutdown.Both);
_socket.Disconnect(true);
}
Does anyone know how to avoid this error?
Explanation here : Only one usage of each socket address (protocol/network address/port) is normally permitted
Thanks @competent_tech