/// <summary>
/// Begins an asynchronous request for a connection to a remote host.
/// </summary>
///
/// <returns>
/// Returns true if the I/O operation is pending. The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will be raised upon completion of the operation. Returns false if the I/O operation completed synchronously. In this case, The <see cref="E:System.Net.Sockets.SocketAsyncEventArgs.Completed"/> event on the <paramref name="e"/> parameter will not be raised and the <paramref name="e"/> object passed as a parameter may be examined immediately after the method call returns to retrieve the result of the operation.
/// </returns>
/// <param name="e">The <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object to use for this asynchronous socket operation.</param><exception cref="T:System.ArgumentException">An argument is not valid. This exception occurs if multiple buffers are specified, the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.BufferList"/> property is not null. </exception><exception cref="T:System.ArgumentNullException">The <paramref name="e"/> parameter cannot be null and the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.RemoteEndPoint"/> cannot be null.</exception><exception cref="T:System.InvalidOperationException">The <see cref="T:System.Net.Sockets.Socket"/> is listening or a socket operation was already in progress using the <see cref="T:System.Net.Sockets.SocketAsyncEventArgs"/> object specified in the <paramref name="e"/> parameter.</exception><exception cref="T:System.Net.Sockets.SocketException">An error occurred when attempting to access the socket. See the Remarks section for more information.</exception><exception cref="T:System.NotSupportedException">Windows XP or later is required for this method. This exception also occurs if the local endpoint and the <see cref="P:System.Net.Sockets.SocketAsyncEventArgs.RemoteEndPoint"/> are not the same address family.</exception><exception cref="T:System.ObjectDisposedException">The <see cref="T:System.Net.Sockets.Socket"/> has been closed. </exception><exception cref="T:System.Security.SecurityException">A caller higher in the call stack does not have permission for the requested operation.</exception>
public bool ConnectAsync(SocketAsyncEventArgs e)
{
if (Socket.s_LoggingEnabled)
Logging.Enter(Logging.Sockets, (object) this, "ConnectAsync", "");
if (this.CleanedUp)
throw new ObjectDisposedException(this.GetType().FullName);
if (e.m_BufferList != null)
throw new ArgumentException(SR.GetString("net_multibuffernotsupported"), "BufferList");
if (e.RemoteEndPoint == null)
throw new ArgumentNullException("remoteEP");
if (this.isListening)
throw new InvalidOperationException(SR.GetString("net_sockets_mustnotlisten"));
EndPoint remoteEndPoint = e.RemoteEndPoint;
DnsEndPoint endPoint1 = remoteEndPoint as DnsEndPoint;
bool flag;
if (endPoint1 != null)
{
if (Socket.s_LoggingEnabled)
Logging.PrintInfo(Logging.Sockets, "Socket#" + ValidationHelper.HashString((object) this) + "::ConnectAsync " + SR.GetString("net_log_socket_connect_dnsendpoint"));
if (endPoint1.AddressFamily != AddressFamily.Unspecified && !this.CanTryAddressFamily(endPoint1.AddressFamily))
throw new NotSupportedException(SR.GetString("net_invalidversion"));
MultipleConnectAsync args = (MultipleConnectAsync) new SingleSocketMultipleConnectAsync(this, true);
e.StartOperationCommon(this);
e.StartOperationWrapperConnect(args);
flag = args.StartConnectAsync(e, endPoint1);
}
else
{
if (!this.CanTryAddressFamily(e.RemoteEndPoint.AddressFamily))
throw new NotSupportedException(SR.GetString("net_invalidversion"));
e.m_SocketAddress = this.CheckCacheRemote(ref remoteEndPoint, false);
if (this.m_RightEndPoint == null)
{
if (remoteEndPoint.AddressFamily == AddressFamily.InterNetwork)
this.InternalBind((EndPoint) new IPEndPoint(IPAddress.Any, 0));
else
this.InternalBind((EndPoint) new IPEndPoint(IPAddress.IPv6Any, 0));
}
EndPoint endPoint2 = this.m_RightEndPoint;
if (this.m_RightEndPoint == null)
this.m_RightEndPoint = remoteEndPoint;
e.StartOperationCommon(this);
e.StartOperationConnect();
this.BindToCompletionPort();
SocketError socketError = SocketError.Success;
int bytesSent;
try
{
if (!this.ConnectEx(this.m_Handle, e.m_PtrSocketAddressBuffer, e.m_SocketAddress.m_Size, e.m_PtrSingleBuffer, e.Count, out bytesSent, (SafeHandle) e.m_PtrNativeOverlapped))
socketError = (SocketError) Marshal.GetLastWin32Error();
}
catch (Exception ex)
{
this.m_RightEndPoint = endPoint2;
e.Complete();
throw ex;
}
if (socketError != SocketError.Success && socketError != SocketError.IOPending)
{
e.FinishOperationSyncFailure(socketError, bytesSent, SocketFlags.None);
flag = false;
}
else
flag = true;
}
if (Socket.s_LoggingEnabled)
Logging.Exit(Logging.Sockets, (object) this, "ConnectAsync", (object) (bool) (flag ? 1 : 0));
return flag;
}