public void EndConnect(IAsyncResult asyncResult)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this, asyncResult);
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
if (asyncResult == null)
{
throw new ArgumentNullException(nameof(asyncResult));
}
LazyAsyncResult castedAsyncResult = null;
EndPoint remoteEndPoint = null;
ConnectOverlappedAsyncResult coar;
MultipleAddressConnectAsyncResult macar;
ConnectAsyncResult car;
coar = asyncResult as ConnectOverlappedAsyncResult;
if (coar == null)
{
macar = asyncResult as MultipleAddressConnectAsyncResult;
if (macar == null)
{
car = asyncResult as ConnectAsyncResult;
if (car != null)
{
remoteEndPoint = car.RemoteEndPoint;
castedAsyncResult = car;
}
}
else
{
remoteEndPoint = macar.RemoteEndPoint;
castedAsyncResult = macar;
}
}
else
{
remoteEndPoint = coar.RemoteEndPoint;
castedAsyncResult = coar;
}
if (castedAsyncResult == null || castedAsyncResult.AsyncObject != this)
{
throw new ArgumentException(SR.net_io_invalidasyncresult, nameof(asyncResult));
}
if (castedAsyncResult.EndCalled)
{
throw new InvalidOperationException(SR.Format(SR.net_io_invalidendcall, "EndConnect"));
}
castedAsyncResult.InternalWaitForCompletion();
castedAsyncResult.EndCalled = true;
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"asyncResult:{asyncResult}");
if (castedAsyncResult.Result is Exception)
{
if (NetEventSource.IsEnabled) NetEventSource.Error(this, castedAsyncResult.Result);
throw (Exception)castedAsyncResult.Result;
}
if ((SocketError)castedAsyncResult.ErrorCode != SocketError.Success)
{
// Update the internal state of this socket according to the error before throwing.
SocketException socketException = SocketExceptionFactory.CreateSocketException(castedAsyncResult.ErrorCode, remoteEndPoint);
UpdateStatusAfterSocketError(socketException);
if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
throw socketException;
}
if (NetEventSource.IsEnabled)
{
NetEventSource.Connected(this, LocalEndPoint, RemoteEndPoint);
NetEventSource.Exit(this, "");
}
}