public void Bind(EndPoint localEP)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this, localEP);
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
// Validate input parameters.
if (localEP == null)
{
throw new ArgumentNullException(nameof(localEP));
}
if (NetEventSource.IsEnabled) NetEventSource.Info(this, $"localEP:{localEP}");
EndPoint endPointSnapshot = localEP;
IPEndPoint ipSnapshot = localEP as IPEndPoint;
// For now security is implemented only on IPEndPoint.
// If EndPoint is of any other type, unmanaged code permission is demanded.
if (ipSnapshot != null)
{
// Take a snapshot that will make it immutable and not derived.
ipSnapshot = ipSnapshot.Snapshot();
endPointSnapshot = RemapIPEndPoint(ipSnapshot);
// NB: if local port is 0, then winsock will assign some port > 1024,
// which is assumed to be safe.
}
// Ask the EndPoint to generate a SocketAddress that we can pass down to native code.
Internals.SocketAddress socketAddress = CallSerializeCheckDnsEndPoint(endPointSnapshot);
DoBind(endPointSnapshot, socketAddress);
if (NetEventSource.IsEnabled) NetEventSource.Exit(this);
}