public IConnection Connect(Uri uri, TimeSpan timeout)
 {
     if (DiagnosticUtility.ShouldTraceInformation)
     {
         TraceUtility.TraceEvent(TraceEventType.Information, 0x4002b, System.ServiceModel.SR.GetString("TraceCodeInitiatingTcpConnection"), new StringTraceRecord("Uri", uri.ToString()), this, null);
     }
     int port = uri.Port;
     IPAddress[] iPAddresses = GetIPAddresses(uri);
     Socket socket = null;
     SocketException innerException = null;
     if (port == -1)
     {
         port = 0x328;
     }
     int invalidAddressCount = 0;
     TimeoutHelper helper = new TimeoutHelper(timeout);
     for (int i = 0; i < iPAddresses.Length; i++)
     {
         if (helper.RemainingTime() == TimeSpan.Zero)
         {
             throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateTimeoutException(uri, helper.OriginalTimeout, iPAddresses, invalidAddressCount, innerException));
         }
         AddressFamily addressFamily = iPAddresses[i].AddressFamily;
         if ((addressFamily == AddressFamily.InterNetworkV6) && !Socket.OSSupportsIPv6)
         {
             iPAddresses[i] = null;
         }
         else
         {
             DateTime utcNow = DateTime.UtcNow;
             try
             {
                 socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
                 socket.Connect(new IPEndPoint(iPAddresses[i], port));
                 innerException = null;
                 break;
             }
             catch (SocketException exception2)
             {
                 invalidAddressCount++;
                 TraceConnectFailure(socket, exception2, uri, (TimeSpan) (DateTime.UtcNow - utcNow));
                 innerException = exception2;
                 socket.Close();
             }
         }
     }
     if (socket == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new EndpointNotFoundException(System.ServiceModel.SR.GetString("NoIPEndpointsFoundForHost", new object[] { uri.Host })));
     }
     if (innerException != null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ConvertConnectException(innerException, uri, helper.ElapsedTime(), innerException));
     }
     return this.CreateConnection(socket);
 }
        public async Task<IConnection> ConnectAsync(Uri uri, TimeSpan timeout)
        {
            int port = uri.Port;
            IPAddress[] addresses = await SocketConnectionInitiator.GetIPAddressesAsync(uri);
            IConnection socketConnection = null;
            SocketException lastException = null;

            if (port == -1)
            {
                port = TcpUri.DefaultPort;
            }

            int invalidAddressCount = 0;
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
            for (int i = 0; i < addresses.Length; i++)
            {
                if (timeoutHelper.RemainingTime() == TimeSpan.Zero)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        CreateTimeoutException(uri, timeoutHelper.OriginalTimeout, addresses, invalidAddressCount, lastException));
                }

                DateTime connectStartTime = DateTime.UtcNow;
                try
                {
                    socketConnection = await CreateConnectionAsync(addresses[i], port);
                    lastException = null;
                    break;
                }
                catch (SocketException socketException)
                {
                    invalidAddressCount++;
                    lastException = socketException;
                }
            }

            if (socketConnection == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new EndpointNotFoundException(SR.Format(SR.NoIPEndpointsFoundForHost, uri.Host)));
            }

            if (lastException != null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    SocketConnectionInitiator.ConvertConnectException(lastException, uri,
                    timeoutHelper.ElapsedTime(), lastException));
            }

            return socketConnection;
        }
        public IConnection Connect(Uri uri, TimeSpan timeout)
        {
            if (DiagnosticUtility.ShouldTraceInformation)
            {
                TraceUtility.TraceEvent(TraceEventType.Information, TraceCode.InitiatingTcpConnection,
                    SR.GetString(SR.TraceCodeInitiatingTcpConnection),
                    new StringTraceRecord("Uri", uri.ToString()), this, null);
            }

            int port = uri.Port;
            IPAddress[] addresses = SocketConnectionInitiator.GetIPAddresses(uri);
            Socket socket = null;
            SocketException lastException = null;

            if (port == -1)
            {
                port = TcpUri.DefaultPort;
            }

            int invalidAddressCount = 0;
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
            for (int i = 0; i < addresses.Length; i++)
            {
                if (timeoutHelper.RemainingTime() == TimeSpan.Zero)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                        CreateTimeoutException(uri, timeoutHelper.OriginalTimeout, addresses, invalidAddressCount, lastException));
                }

                AddressFamily addressFamily = addresses[i].AddressFamily;

                if (addressFamily == AddressFamily.InterNetworkV6 && !Socket.OSSupportsIPv6)
                {
                    addresses[i] = null; // disregard for exception attempt purposes
                    continue;
                }

                DateTime connectStartTime = DateTime.UtcNow;
                try
                {
                    socket = new Socket(addressFamily, SocketType.Stream, ProtocolType.Tcp);
                    socket.Connect(new IPEndPoint(addresses[i], port));
                    lastException = null;
                    break;
                }
                catch (SocketException socketException)
                {
                    invalidAddressCount++;
                    SocketConnectionInitiator.TraceConnectFailure(socket, socketException, uri, DateTime.UtcNow - connectStartTime);
                    lastException = socketException;
                    socket.Close();
                }
            }

            if (socket == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    new EndpointNotFoundException(SR.GetString(SR.NoIPEndpointsFoundForHost, uri.Host)));
            }

            if (lastException != null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                    SocketConnectionInitiator.ConvertConnectException(lastException, uri,
                    timeoutHelper.ElapsedTime(), lastException));
            }

            return CreateConnection(socket);
        }