private static void ConnectSocketCallback(IAsyncResult asyncResult)
        {
            ConnectSocketState asyncState     = (ConnectSocketState)asyncResult.AsyncState;
            Socket             socket         = null;
            IPAddress          address        = null;
            Exception          exception      = null;
            Exception          e              = null;
            WebExceptionStatus connectFailure = WebExceptionStatus.ConnectFailure;

            try
            {
                connectFailure = asyncState.servicePoint.ConnectSocketInternal(asyncState.connectFailure, asyncState.s4, asyncState.s6, ref socket, ref address, asyncState, asyncResult, -1, out exception);
            }
            catch (SocketException exception3)
            {
                e = exception3;
            }
            catch (ObjectDisposedException exception4)
            {
                e = exception4;
            }
            switch (connectFailure)
            {
            case WebExceptionStatus.Pending:
                return;

            case WebExceptionStatus.Success:
                try
                {
                    asyncState.servicePoint.CompleteGetConnection(asyncState.s4, asyncState.s6, socket, address);
                }
                catch (SocketException exception5)
                {
                    e = exception5;
                }
                catch (ObjectDisposedException exception6)
                {
                    e = exception6;
                }
                break;

            default:
                e = new WebException(NetRes.GetWebStatusString(connectFailure), ((connectFailure == WebExceptionStatus.ProxyNameResolutionFailure) || (connectFailure == WebExceptionStatus.NameResolutionFailure)) ? asyncState.servicePoint.Host : null, exception, connectFailure, null, WebExceptionInternalStatus.ServicePointFatal);
                break;
            }
            try
            {
                asyncState.pooledStream.ConnectionCallback(asyncState.owner, e, socket, address);
            }
            catch
            {
                if ((socket == null) || !socket.CleanedUp)
                {
                    throw;
                }
            }
        }
        internal Socket GetConnection(PooledStream PooledStream, object owner, bool async, out IPAddress address, ref Socket abortSocket, ref Socket abortSocket6, int timeout)
        {
            Socket             socket         = null;
            Socket             socket2        = null;
            Socket             socket3        = null;
            Exception          exception      = null;
            WebExceptionStatus connectFailure = WebExceptionStatus.ConnectFailure;

            address = null;
            if (Socket.OSSupportsIPv4)
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            }
            if (Socket.OSSupportsIPv6)
            {
                socket2 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
            }
            abortSocket  = socket;
            abortSocket6 = socket2;
            ConnectSocketState state = null;

            if (async)
            {
                state = new ConnectSocketState(this, PooledStream, owner, socket, socket2);
            }
            connectFailure = this.ConnectSocket(socket, socket2, ref socket3, ref address, state, timeout, out exception);
            if (connectFailure == WebExceptionStatus.Pending)
            {
                return(null);
            }
            if (connectFailure != WebExceptionStatus.Success)
            {
                throw new WebException(NetRes.GetWebStatusString(connectFailure), ((connectFailure == WebExceptionStatus.ProxyNameResolutionFailure) || (connectFailure == WebExceptionStatus.NameResolutionFailure)) ? this.Host : null, exception, connectFailure, null, WebExceptionInternalStatus.ServicePointFatal);
            }
            if (socket3 == null)
            {
                throw new IOException(SR.GetString("net_io_transportfailure"));
            }
            this.CompleteGetConnection(socket, socket2, socket3, address);
            return(socket3);
        }
 internal Socket GetConnection(PooledStream PooledStream, object owner, bool async, out IPAddress address, ref Socket abortSocket, ref Socket abortSocket6, int timeout)
 {
     Socket socket = null;
     Socket socket2 = null;
     Socket socket3 = null;
     Exception exception = null;
     WebExceptionStatus connectFailure = WebExceptionStatus.ConnectFailure;
     address = null;
     if (Socket.OSSupportsIPv4)
     {
         socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
     }
     if (Socket.OSSupportsIPv6)
     {
         socket2 = new Socket(AddressFamily.InterNetworkV6, SocketType.Stream, ProtocolType.Tcp);
     }
     abortSocket = socket;
     abortSocket6 = socket2;
     ConnectSocketState state = null;
     if (async)
     {
         state = new ConnectSocketState(this, PooledStream, owner, socket, socket2);
     }
     connectFailure = this.ConnectSocket(socket, socket2, ref socket3, ref address, state, timeout, out exception);
     if (connectFailure == WebExceptionStatus.Pending)
     {
         return null;
     }
     if (connectFailure != WebExceptionStatus.Success)
     {
         throw new WebException(NetRes.GetWebStatusString(connectFailure), ((connectFailure == WebExceptionStatus.ProxyNameResolutionFailure) || (connectFailure == WebExceptionStatus.NameResolutionFailure)) ? this.Host : null, exception, connectFailure, null, WebExceptionInternalStatus.ServicePointFatal);
     }
     if (socket3 == null)
     {
         throw new IOException(SR.GetString("net_io_transportfailure"));
     }
     this.CompleteGetConnection(socket, socket2, socket3, address);
     return socket3;
 }
 private WebExceptionStatus ConnectSocketInternal(bool connectFailure, Socket s4, Socket s6, ref Socket socket, ref IPAddress address, ConnectSocketState state, IAsyncResult asyncResult, int timeout, out Exception exception)
 {
     exception = null;
     bool timedOut = false;
     IPAddress[] addresses = null;
     for (int i = 0; i < 2; i++)
     {
         int currentIndex;
         int num3 = 0;
         if (asyncResult == null)
         {
             addresses = this.GetIPAddressInfoList(out currentIndex, addresses, timeout, out timedOut);
             if (((addresses == null) || (addresses.Length == 0)) || timedOut)
             {
                 break;
             }
         }
         else
         {
             addresses = state.addresses;
             currentIndex = state.currentIndex;
             num3 = state.i;
             i = state.unsuccessfulAttempts;
         }
         while (num3 < addresses.Length)
         {
             IPAddress address2 = addresses[currentIndex];
             try
             {
                 Socket socket2;
                 IPEndPoint remoteIPEndPoint = new IPEndPoint(address2, this.m_Port);
                 if (remoteIPEndPoint.Address.AddressFamily == AddressFamily.InterNetwork)
                 {
                     socket2 = s4;
                 }
                 else
                 {
                     socket2 = s6;
                 }
                 if (state != null)
                 {
                     if (asyncResult == null)
                     {
                         state.addresses = addresses;
                         state.currentIndex = currentIndex;
                         state.i = num3;
                         state.unsuccessfulAttempts = i;
                         state.connectFailure = connectFailure;
                         if ((this.BindIPEndPointDelegate != null) && !socket2.IsBound)
                         {
                             this.BindUsingDelegate(socket2, remoteIPEndPoint);
                         }
                         socket2.UnsafeBeginConnect(remoteIPEndPoint, m_ConnectCallbackDelegate, state);
                         return WebExceptionStatus.Pending;
                     }
                     IAsyncResult result = asyncResult;
                     asyncResult = null;
                     socket2.EndConnect(result);
                 }
                 else
                 {
                     if ((this.BindIPEndPointDelegate != null) && !socket2.IsBound)
                     {
                         this.BindUsingDelegate(socket2, remoteIPEndPoint);
                     }
                     socket2.InternalConnect(remoteIPEndPoint);
                 }
                 socket = socket2;
                 address = address2;
                 exception = null;
                 this.UpdateCurrentIndex(addresses, currentIndex);
                 return WebExceptionStatus.Success;
             }
             catch (ObjectDisposedException)
             {
                 return WebExceptionStatus.RequestCanceled;
             }
             catch (Exception exception2)
             {
                 if (NclUtilities.IsFatal(exception2))
                 {
                     throw;
                 }
                 exception = exception2;
                 connectFailure = true;
             }
             currentIndex++;
             if (currentIndex >= addresses.Length)
             {
                 currentIndex = 0;
             }
             num3++;
         }
     }
     this.Failed(addresses);
     if (connectFailure)
     {
         return WebExceptionStatus.ConnectFailure;
     }
     if (timedOut)
     {
         return WebExceptionStatus.Timeout;
     }
     if (!this.InternalProxyServicePoint)
     {
         return WebExceptionStatus.NameResolutionFailure;
     }
     return WebExceptionStatus.ProxyNameResolutionFailure;
 }
 private WebExceptionStatus ConnectSocket(Socket s4, Socket s6, ref Socket socket, ref IPAddress address, ConnectSocketState state, int timeout, out Exception exception)
 {
     return this.ConnectSocketInternal(false, s4, s6, ref socket, ref address, state, null, timeout, out exception);
 }
        /// <devdoc>
        ///    <para>
        ///     Tempory for getting a new Connection for FTP client, for the time being
        ///    </para>
        /// </devdoc>
        internal Socket GetConnection(PooledStream PooledStream, object owner, bool async, out IPAddress address, ref Socket abortSocket, ref Socket abortSocket6)
        {
            Socket socket = null;
            Socket socket6 = null;
            Socket finalSocket = null;
            Exception innerException = null;
            WebExceptionStatus ws = WebExceptionStatus.ConnectFailure;
            address = null;

            //
            // if we will not create a tunnel through a proxy then create
            // and connect the socket we will use for the connection
            //

            //
            // IPv6 Support: If IPv6 is enabled, then we create a second socket that ServicePoint
            //               will use if it wants to connect via IPv6.
            //
            if ( Socket.OSSupportsIPv4 ) {
                socket = new Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp);
            }

            if ( Socket.OSSupportsIPv6 ) {
                socket6 = new Socket(AddressFamily.InterNetworkV6,SocketType.Stream,ProtocolType.Tcp);
            }

            abortSocket = socket;
            abortSocket6 = socket6;

            //
            // Setup socket timeouts for [....] requests
            //
            // 

            ConnectSocketState state = null;

            if (async) {
                state = new ConnectSocketState(this, PooledStream, owner, socket, socket6);
            }

            ws = ConnectSocket(socket, socket6, ref finalSocket, ref address, state, out innerException);

            if (ws == WebExceptionStatus.Pending) {
                return null;
            }

            if (ws != WebExceptionStatus.Success) {
                throw new WebException(
                    NetRes.GetWebStatusString(ws),
                    ws == WebExceptionStatus.ProxyNameResolutionFailure || ws == WebExceptionStatus.NameResolutionFailure ? Host : null,
                    innerException,
                    ws,
                    null, /* no response */
                    WebExceptionInternalStatus.ServicePointFatal);
            }

            //
            // There should be no means for socket to be null at this
            // point, but the damage is greater if we just carry on
            // without ensuring that it's good.
            //
            if ( finalSocket == null ) {
                throw new IOException(SR.GetString(SR.net_io_transportfailure));
            }

            CompleteGetConnection(socket, socket6, finalSocket, address);
            return finalSocket;
        }
 /// <summary>
 ///    <para>private implimentation of ConnectSocket(...)</para>
 /// </summary>
 private WebExceptionStatus ConnectSocket(Socket s4, Socket s6, ref Socket socket, ref IPAddress address, 
     ConnectSocketState state, out Exception exception) {
     //
     // we need this for the call to connect()
     //
     return ConnectSocketInternal(false, s4, s6, ref socket, ref address, state, null, out exception);
 }
        /// <summary>
        ///    <para>This is the real logic for doing the Connect with IPv4 and IPv6 addresses, see ConnectSocket for details</para>
        /// </summary>
        private WebExceptionStatus ConnectSocketInternal(bool connectFailure, Socket s4, Socket s6, ref Socket socket, 
            ref IPAddress address, ConnectSocketState state, IAsyncResult asyncResult, out Exception exception) {
            IPEndPoint remoteIPEndPoint;
            exception = null;

            //
            // so, here we are: we have the EndPoint we need to connect to, all we
            // need to do is call into winsock and try to connect to this HTTP server.
            //
            // this is how we do it:
            // we'll keep trying to Connect() until either:
            // (1) Connect() succeeds (on which we increment the number of connections opened) or
            // (2) we can't get any new address for this host.
            //
            // (1) is pretty easy, here's how we do (2):
            // If the hostinformation is every marked as failed, we will automatically refresh it
            // the next time it is read.
            // If we fail the first time using the DNS information and the DNS information is recent,
            // which mean's it either hasn't been tried or it failed, we will mark the
            // hostinformation as failed, and quit.  Otherwise we'll refresh the DNS information and
            // try one more time. If we fail the second time, then we'll mark the DNS information
            // as failed and quit.
            IPAddress[] addresses = null;
            for (int unsuccessfulAttempts = 0; unsuccessfulAttempts < 2; unsuccessfulAttempts++) {

                int currentIndex;
                int i = 0;

                // Use asyncResult to make sure it is only called at initiation time.
                if (asyncResult == null)
                {
                    // the second time, determine if the list was recent.

                    addresses = GetIPAddressInfoList(out currentIndex, addresses);

                    //the addresses were recent, or we couldn't resolve the addresses.
                    if (addresses == null || addresses.Length == 0)
                        break;
                }
                else
                {
                    GlobalLog.Print("ServicePoint#" + ValidationHelper.HashString(this) + "::ConnectSocketInternal() resuming previous state");

                    addresses = state.addresses;
                    currentIndex = state.currentIndex;
                    i = state.i;
                    unsuccessfulAttempts = state.unsuccessfulAttempts;
                }

                //otherwise, try all of the addresses in the list.
                for (; i < addresses.Length; i++)
                {
                    IPAddress ipAddressInfo = addresses[currentIndex];
                    try {
                        remoteIPEndPoint = new IPEndPoint(ipAddressInfo, m_Port);
                        Socket attemptSocket;
                        if ( remoteIPEndPoint.Address.AddressFamily==AddressFamily.InterNetwork ) {
                            attemptSocket = s4;
                        }
                        else {
                            attemptSocket = s6;
                        }

                        if (state != null)
                        {
                            if (asyncResult != null)
                            {
                                IAsyncResult asyncResultCopy = asyncResult;
                                asyncResult = null;
                                attemptSocket.EndConnect(asyncResultCopy);
                            }
                            else {
                                GlobalLog.Print("ServicePoint#" + ValidationHelper.HashString(this) + "::ConnectSocketInternal() calling BeginConnect() to:" + remoteIPEndPoint.ToString());

                                // save off our state and do our async call
                                state.addresses = addresses;
                                state.currentIndex = currentIndex;
                                state.i = i;
                                state.unsuccessfulAttempts = unsuccessfulAttempts;
                                state.connectFailure = connectFailure;

                                if (BindIPEndPointDelegate != null && !attemptSocket.IsBound) {
                                    BindUsingDelegate(attemptSocket, remoteIPEndPoint);
                                }

                                attemptSocket.UnsafeBeginConnect(remoteIPEndPoint, m_ConnectCallbackDelegate, state);
                                return WebExceptionStatus.Pending;
                            }
                        }
                        else {
                            if (BindIPEndPointDelegate != null && !attemptSocket.IsBound) {
                                BindUsingDelegate(attemptSocket, remoteIPEndPoint);
                            }

                            attemptSocket.InternalConnect(remoteIPEndPoint);
                        }
                        socket  = attemptSocket;
                        address = ipAddressInfo;
                        exception = null;
                        UpdateCurrentIndex(addresses, currentIndex);
                        return WebExceptionStatus.Success;
                    }
                    catch (ObjectDisposedException)
                    {
                        // This can happen if the request has been aborted and the attemptSocket got closed.
                        return WebExceptionStatus.RequestCanceled;
                    }
                    catch (Exception e)
                    {
                        if (NclUtilities.IsFatal(e)) throw;

                        exception = e;
                        connectFailure = true;
                    }
                    currentIndex++;
                    if (currentIndex >= addresses.Length) {
                        currentIndex = 0;
                    }
                }
            }

            Failed(addresses);

            return connectFailure ? WebExceptionStatus.ConnectFailure :
                InternalProxyServicePoint ? WebExceptionStatus.ProxyNameResolutionFailure :
                WebExceptionStatus.NameResolutionFailure;
        }
        private WebExceptionStatus ConnectSocketInternal(bool connectFailure, Socket s4, Socket s6, ref Socket socket, ref IPAddress address, ConnectSocketState state, IAsyncResult asyncResult, int timeout, out Exception exception)
        {
            exception = null;
            bool timedOut = false;

            IPAddress[] addresses = null;
            for (int i = 0; i < 2; i++)
            {
                int currentIndex;
                int num3 = 0;
                if (asyncResult == null)
                {
                    addresses = this.GetIPAddressInfoList(out currentIndex, addresses, timeout, out timedOut);
                    if (((addresses == null) || (addresses.Length == 0)) || timedOut)
                    {
                        break;
                    }
                }
                else
                {
                    addresses    = state.addresses;
                    currentIndex = state.currentIndex;
                    num3         = state.i;
                    i            = state.unsuccessfulAttempts;
                }
                while (num3 < addresses.Length)
                {
                    IPAddress address2 = addresses[currentIndex];
                    try
                    {
                        Socket     socket2;
                        IPEndPoint remoteIPEndPoint = new IPEndPoint(address2, this.m_Port);
                        if (remoteIPEndPoint.Address.AddressFamily == AddressFamily.InterNetwork)
                        {
                            socket2 = s4;
                        }
                        else
                        {
                            socket2 = s6;
                        }
                        if (state != null)
                        {
                            if (asyncResult == null)
                            {
                                state.addresses            = addresses;
                                state.currentIndex         = currentIndex;
                                state.i                    = num3;
                                state.unsuccessfulAttempts = i;
                                state.connectFailure       = connectFailure;
                                if ((this.BindIPEndPointDelegate != null) && !socket2.IsBound)
                                {
                                    this.BindUsingDelegate(socket2, remoteIPEndPoint);
                                }
                                socket2.UnsafeBeginConnect(remoteIPEndPoint, m_ConnectCallbackDelegate, state);
                                return(WebExceptionStatus.Pending);
                            }
                            IAsyncResult result = asyncResult;
                            asyncResult = null;
                            socket2.EndConnect(result);
                        }
                        else
                        {
                            if ((this.BindIPEndPointDelegate != null) && !socket2.IsBound)
                            {
                                this.BindUsingDelegate(socket2, remoteIPEndPoint);
                            }
                            socket2.InternalConnect(remoteIPEndPoint);
                        }
                        socket    = socket2;
                        address   = address2;
                        exception = null;
                        this.UpdateCurrentIndex(addresses, currentIndex);
                        return(WebExceptionStatus.Success);
                    }
                    catch (ObjectDisposedException)
                    {
                        return(WebExceptionStatus.RequestCanceled);
                    }
                    catch (Exception exception2)
                    {
                        if (NclUtilities.IsFatal(exception2))
                        {
                            throw;
                        }
                        exception      = exception2;
                        connectFailure = true;
                    }
                    currentIndex++;
                    if (currentIndex >= addresses.Length)
                    {
                        currentIndex = 0;
                    }
                    num3++;
                }
            }
            this.Failed(addresses);
            if (connectFailure)
            {
                return(WebExceptionStatus.ConnectFailure);
            }
            if (timedOut)
            {
                return(WebExceptionStatus.Timeout);
            }
            if (!this.InternalProxyServicePoint)
            {
                return(WebExceptionStatus.NameResolutionFailure);
            }
            return(WebExceptionStatus.ProxyNameResolutionFailure);
        }
 private WebExceptionStatus ConnectSocket(Socket s4, Socket s6, ref Socket socket, ref IPAddress address, ConnectSocketState state, int timeout, out Exception exception)
 {
     return(this.ConnectSocketInternal(false, s4, s6, ref socket, ref address, state, null, timeout, out exception));
 }