private void DoBind(EndPoint endPointSnapshot, Internals.SocketAddress socketAddress)
{
// Mitigation for Blue Screen of Death (Win7, maybe others).
IPEndPoint ipEndPoint = endPointSnapshot as IPEndPoint;
if (!OSSupportsIPv4 && ipEndPoint != null && ipEndPoint.Address.IsIPv4MappedToIPv6)
{
SocketException socketException = new SocketException((int)SocketError.InvalidArgument);
UpdateStatusAfterSocketError(socketException);
if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
throw socketException;
}
// This may throw ObjectDisposedException.
SocketError errorCode = SocketPal.Bind(
_handle,
socketAddress.Buffer,
socketAddress.Size);
#if TRACE_VERBOSE
if (NetEventSource.IsEnabled)
{
try
{
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"SRC:{LocalEndPoint} Interop.Winsock.bind returns errorCode:{errorCode}");
}
catch (ObjectDisposedException) { }
}
#endif
// Throw an appropriate SocketException if the native call fails.
if (errorCode != SocketError.Success)
{
// Update the internal state of this socket according to the error before throwing.
SocketException socketException = new SocketException((int)errorCode);
UpdateStatusAfterSocketError(socketException);
if (NetEventSource.IsEnabled) NetEventSource.Error(this, socketException);
throw socketException;
}
if (_rightEndPoint == null)
{
// Save a copy of the EndPoint so we can use it for Create().
_rightEndPoint = endPointSnapshot;
}
}