public IAsyncResult BeginConnect(string host, int port, AsyncCallback requestCallback, object state)
{
if (NetEventSource.IsEnabled) NetEventSource.Enter(this, host);
if (CleanedUp)
{
throw new ObjectDisposedException(this.GetType().FullName);
}
if (host == null)
{
throw new ArgumentNullException(nameof(host));
}
if (!TcpValidationHelpers.ValidatePortNumber(port))
{
throw new ArgumentOutOfRangeException(nameof(port));
}
if (_addressFamily != AddressFamily.InterNetwork && _addressFamily != AddressFamily.InterNetworkV6)
{
throw new NotSupportedException(SR.net_invalidversion);
}
if (_isListening)
{
throw new InvalidOperationException(SR.net_sockets_mustnotlisten);
}
IPAddress parsedAddress;
if (IPAddress.TryParse(host, out parsedAddress))
{
IAsyncResult r = BeginConnect(parsedAddress, port, requestCallback, state);
if (NetEventSource.IsEnabled) NetEventSource.Exit(this, r);
return r;
}
ThrowIfNotSupportsMultipleConnectAttempts();
// Here, want to flow the context. No need to lock.
MultipleAddressConnectAsyncResult result = new MultipleAddressConnectAsyncResult(null, port, this, state, requestCallback);
result.StartPostingAsyncOp(false);
IAsyncResult dnsResult = DnsAPMExtensions.BeginGetHostAddresses(host, new AsyncCallback(DnsCallback), result);
if (dnsResult.CompletedSynchronously)
{
if (DoDnsCallback(dnsResult, result))
{
result.InvokeCallback();
}
}
// Done posting.
result.FinishPostingAsyncOp(ref Caches.ConnectClosureCache);
if (NetEventSource.IsEnabled) NetEventSource.Exit(this, result);
return result;
}