public static bool ConnectAsync(SocketType socketType, ProtocolType protocolType, SocketAsyncEventArgs e)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(null);
bool retval;
if (e == null)
{
throw new ArgumentNullException(nameof(e));
}
if (e._bufferList != null)
{
throw new ArgumentException(SR.net_multibuffernotsupported, "BufferList");
}
if (e.RemoteEndPoint == null)
{
throw new ArgumentNullException("remoteEP");
}
EndPoint endPointSnapshot = e.RemoteEndPoint;
DnsEndPoint dnsEP = endPointSnapshot as DnsEndPoint;
if (dnsEP != null)
{
Socket attemptSocket = null;
MultipleConnectAsync multipleConnectAsync = null;
if (dnsEP.AddressFamily == AddressFamily.Unspecified)
{
// Disable CS0162 and CS0429: Unreachable code detected
//
// SupportsMultipleConnectAttempts is a constant; when false, the following lines will trigger CS0162 or CS0429.
//
// This is the only *Connect* API that actually supports multiple endpoint attempts, as it's responsible
// for creating each Socket instance and can create one per attempt (with the instance methods, once a
// connect fails, on unix systems that socket can't be used for subsequent connect attempts).
#pragma warning disable 162, 429
multipleConnectAsync = SocketPal.SupportsMultipleConnectAttempts ?
(MultipleConnectAsync)(new DualSocketMultipleConnectAsync(socketType, protocolType)) :
(MultipleConnectAsync)(new MultipleSocketMultipleConnectAsync(socketType, protocolType));
#pragma warning restore
}
else
{
attemptSocket = new Socket(dnsEP.AddressFamily, socketType, protocolType);
multipleConnectAsync = new SingleSocketMultipleConnectAsync(attemptSocket, false);
}
e.StartOperationCommon(attemptSocket);
e.StartOperationWrapperConnect(multipleConnectAsync);
retval = multipleConnectAsync.StartConnectAsync(e, dnsEP);
}
else
{
Socket attemptSocket = new Socket(endPointSnapshot.AddressFamily, socketType, protocolType);
retval = attemptSocket.ConnectAsync(e);
}
if (NetEventSource.IsEnabled) NetEventSource.Exit(null, retval);
return retval;
}