System.Net.Connection.TunnelThroughProxyWrapper C# (CSharp) Method

TunnelThroughProxyWrapper() private static method

private static TunnelThroughProxyWrapper ( IAsyncResult result ) : void
result IAsyncResult
return void
        private static void TunnelThroughProxyWrapper(IAsyncResult result){
            if(result.CompletedSynchronously){
                return;
            }

            bool success = false;
            WebExceptionStatus ws = WebExceptionStatus.ConnectFailure;
            HttpWebRequest req = (HttpWebRequest)((LazyAsyncResult)result).AsyncObject;
            Connection conn = (Connection)((TunnelStateObject)result.AsyncState).Connection;
            HttpWebRequest originalReq = (HttpWebRequest)((TunnelStateObject)result.AsyncState).OriginalRequest;
            
            GlobalLog.Enter("Connection#" + ValidationHelper.HashString(conn) + "::TunnelThroughProxyCallback");

            try{
                req.EndGetResponse(result);
                HttpWebResponse connectResponse = (HttpWebResponse)req.GetResponse();
                ConnectStream connectStream = (ConnectStream)connectResponse.GetResponseStream();
                
                // this stream will be used as the real stream for TlsStream
                conn.NetworkStream = new NetworkStream(connectStream.Connection.NetworkStream, true);
                // This will orphan the original connect stream now owned by tunnelStream
                connectStream.Connection.NetworkStream.ConvertToNotSocketOwner();
                success = true;
            }

            catch (Exception exception) {
                if (conn.m_InnerException == null)
                    conn.m_InnerException = exception;

                if (exception is WebException) {
                    ws = ((WebException)exception).Status;
                }

                GlobalLog.Print("Connection#" + ValidationHelper.HashString(conn) + "::TunnelThroughProxyCallback() exception occurred: " + exception);
            }
            if(!success)
            {
                ConnectionReturnResult returnResult = null;
                conn.HandleError(false, false, ws, ref returnResult);
                ConnectionReturnResult.SetResponses(returnResult);
                return;
            }

            conn.CompleteConnection(true, originalReq);
            GlobalLog.Leave("Connection#" + ValidationHelper.HashString(conn) + "::TunnelThroughProxyCallback");
        }