private CommandStream.PipelineInstruction QueueOrCreateDataConection(CommandStream.PipelineEntry entry, ResponseDescription response, bool timeout, ref Stream stream, out bool isSocketReady)
        {
            CommandStream.PipelineInstruction instruction2;
            isSocketReady = false;
            if (this.m_DataHandshakeStarted)
            {
                isSocketReady = true;
                return(CommandStream.PipelineInstruction.Pause);
            }
            this.m_DataHandshakeStarted = true;
            bool flag = false;
            int  port = -1;

            if ((entry.Command == "PASV\r\n") || (entry.Command == "EPSV\r\n"))
            {
                if (!response.PositiveCompletion)
                {
                    base.m_AbortReason = SR.GetString("net_ftp_server_failed_passive", new object[] { response.Status });
                    return(CommandStream.PipelineInstruction.Abort);
                }
                if (entry.Command == "PASV\r\n")
                {
                    port = this.GetPortV4(response.StatusDescription);
                }
                else
                {
                    port = this.GetPortV6(response.StatusDescription);
                }
                flag = true;
            }
            new SocketPermission(PermissionState.Unrestricted).Assert();
            try
            {
                CommandStream.PipelineInstruction pause;
                if (flag)
                {
                    try
                    {
                        this.m_DataSocket = this.CreateFtpDataSocket((FtpWebRequest)base.m_Request, base.Socket);
                    }
                    catch (ObjectDisposedException)
                    {
                        throw ExceptionHelper.RequestAbortedException;
                    }
                    this.m_PassiveEndPoint = new IPEndPoint(base.ServerAddress, port);
                }
                if (this.m_PassiveEndPoint != null)
                {
                    IPEndPoint passiveEndPoint = this.m_PassiveEndPoint;
                    this.m_PassiveEndPoint = null;
                    if (base.m_Async)
                    {
                        this.m_DataSocket.BeginConnect(passiveEndPoint, m_ConnectCallbackDelegate, this);
                        pause = CommandStream.PipelineInstruction.Pause;
                    }
                    else
                    {
                        this.m_DataSocket.Connect(passiveEndPoint);
                        pause = CommandStream.PipelineInstruction.Advance;
                    }
                }
                else if (base.m_Async)
                {
                    this.m_DataSocket.BeginAccept(m_AcceptCallbackDelegate, this);
                    pause = CommandStream.PipelineInstruction.Pause;
                }
                else
                {
                    Socket dataSocket = this.m_DataSocket;
                    try
                    {
                        this.m_DataSocket = this.m_DataSocket.Accept();
                        if (!base.ServerAddress.Equals(((IPEndPoint)this.m_DataSocket.RemoteEndPoint).Address))
                        {
                            this.m_DataSocket.Close();
                            throw new WebException(SR.GetString("net_ftp_active_address_different"), WebExceptionStatus.ProtocolError);
                        }
                        isSocketReady = true;
                        pause         = CommandStream.PipelineInstruction.Pause;
                    }
                    finally
                    {
                        dataSocket.Close();
                    }
                }
                instruction2 = pause;
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            return(instruction2);
        }
        protected override CommandStream.PipelineInstruction PipelineCallback(CommandStream.PipelineEntry entry, ResponseDescription response, bool timeout, ref Stream stream)
        {
            if (response == null)
            {
                return(CommandStream.PipelineInstruction.Abort);
            }
            FtpStatusCode status = (FtpStatusCode)response.Status;

            if (status != FtpStatusCode.ClosingControl)
            {
                this.StatusCode = status;
                this.StatusLine = response.StatusDescription;
            }
            if (response.InvalidStatusCode)
            {
                throw new WebException(SR.GetString("net_InvalidStatusCode"), WebExceptionStatus.ProtocolError);
            }
            if (base.m_Index == -1)
            {
                switch (status)
                {
                case FtpStatusCode.SendUserCommand:
                    this.m_BannerMessage = new StringBuilder();
                    this.m_BannerMessage.Append(this.StatusLine);
                    return(CommandStream.PipelineInstruction.Advance);

                case FtpStatusCode.ServiceTemporarilyNotAvailable:
                    return(CommandStream.PipelineInstruction.Reread);
                }
                throw base.GenerateException(status, response.StatusDescription, null);
            }
            if (entry.Command == "OPTS utf8 on\r\n")
            {
                if (response.PositiveCompletion)
                {
                    base.Encoding = Encoding.UTF8;
                }
                else
                {
                    base.Encoding = Encoding.Default;
                }
                return(CommandStream.PipelineInstruction.Advance);
            }
            if (entry.Command.IndexOf("USER") != -1)
            {
                if (status == FtpStatusCode.LoggedInProceed)
                {
                    this.m_LoginState = FtpLoginState.LoggedIn;
                    base.m_Index++;
                }
                else if ((status == FtpStatusCode.NotLoggedIn) && (this.m_LoginState != FtpLoginState.NotLoggedIn))
                {
                    this.m_LoginState = FtpLoginState.ReloginFailed;
                    throw ExceptionHelper.IsolatedException;
                }
            }
            if (response.TransientFailure || response.PermanentFailure)
            {
                if (status == FtpStatusCode.ServiceNotAvailable)
                {
                    base.MarkAsRecoverableFailure();
                }
                throw base.GenerateException(status, response.StatusDescription, null);
            }
            if ((this.m_LoginState != FtpLoginState.LoggedIn) && (entry.Command.IndexOf("PASS") != -1))
            {
                switch (status)
                {
                case FtpStatusCode.NeedLoginAccount:
                case FtpStatusCode.LoggedInProceed:
                    this.m_LoginState = FtpLoginState.LoggedIn;
                    goto Label_017A;
                }
                throw base.GenerateException(status, response.StatusDescription, null);
            }
Label_017A:
            if (entry.HasFlag(CommandStream.PipelineEntryFlags.CreateDataConnection) && (response.PositiveCompletion || response.PositiveIntermediate))
            {
                bool flag;
                CommandStream.PipelineInstruction instruction = this.QueueOrCreateDataConection(entry, response, timeout, ref stream, out flag);
                if (!flag)
                {
                    return(instruction);
                }
            }
            switch (status)
            {
            case FtpStatusCode.OpeningData:
            case FtpStatusCode.DataAlreadyOpen:
            {
                if (this.m_DataSocket == null)
                {
                    return(CommandStream.PipelineInstruction.Abort);
                }
                if (!entry.HasFlag(CommandStream.PipelineEntryFlags.GiveDataStream))
                {
                    base.m_AbortReason = SR.GetString("net_ftp_invalid_status_response", new object[] { status, entry.Command });
                    return(CommandStream.PipelineInstruction.Abort);
                }
                this.TryUpdateContentLength(response.StatusDescription);
                FtpWebRequest request = (FtpWebRequest)base.m_Request;
                if (request.MethodInfo.ShouldParseForResponseUri)
                {
                    this.TryUpdateResponseUri(response.StatusDescription, request);
                }
                return(this.QueueOrCreateFtpDataStream(ref stream));
            }

            case FtpStatusCode.LoggedInProceed:
                this.m_WelcomeMessage.Append(this.StatusLine);
                break;

            case FtpStatusCode.ClosingControl:
                this.m_ExitMessage.Append(response.StatusDescription);
                base.CloseSocket();
                break;

            case FtpStatusCode.ServerWantsSecureSession:
            {
                FtpWebRequest initiatingRequest = (FtpWebRequest)base.m_Request;
                TlsStream     stream2           = new TlsStream(initiatingRequest.RequestUri.Host, base.NetworkStream, initiatingRequest.ClientCertificates, base.Pool.ServicePoint, initiatingRequest, base.m_Async ? initiatingRequest.GetWritingContext().ContextCopy : null);
                base.NetworkStream = stream2;
                break;
            }

            case FtpStatusCode.FileStatus:
            {
                FtpWebRequest request1 = (FtpWebRequest)base.m_Request;
                if (entry.Command.StartsWith("SIZE "))
                {
                    this.m_ContentLength = this.GetContentLengthFrom213Response(response.StatusDescription);
                }
                else if (entry.Command.StartsWith("MDTM "))
                {
                    this.m_LastModified = this.GetLastModifiedFrom213Response(response.StatusDescription);
                }
                break;
            }

            default:
                if (status == FtpStatusCode.PathnameCreated)
                {
                    if ((entry.Command == "PWD\r\n") && !entry.HasFlag(CommandStream.PipelineEntryFlags.UserCommand))
                    {
                        this.m_LoginDirectory = this.GetLoginDirectory(response.StatusDescription);
                    }
                }
                else if (entry.Command.IndexOf("CWD") != -1)
                {
                    this.m_EstablishedServerDirectory = this.m_RequestedServerDirectory;
                }
                break;
            }
            if (!response.PositiveIntermediate && (base.UsingSecureStream || !(entry.Command == "AUTH TLS\r\n")))
            {
                return(CommandStream.PipelineInstruction.Advance);
            }
            return(CommandStream.PipelineInstruction.Reread);
        }