private void EndReceive(IAsyncResult result)
{
try
{
var endPoint = new IPEndPoint(IPAddress.Any, Endpoint.Port);
var buffer = _client.EndReceive(result, ref endPoint);
OnMessageReceived(buffer, endPoint);
_client.BeginReceive(EndReceive, null);
}
catch (ObjectDisposedException)
{
// Ignore, we're finished!
}
catch (SocketException ex)
{
// If the destination computer closes the connection
// we get error code 10054. We need to keep receiving on
// the socket until we clear all the error states
if (ex.ErrorCode == 10054)
{
while (true)
{
try
{
_client.BeginReceive(EndReceive, null);
return;
}
catch (ObjectDisposedException)
{
return;
}
catch (SocketException e)
{
if (e.ErrorCode != 10054)
return;
}
}
}
}
}