System.Net.FtpWebRequest.SubmitRequest C# (CSharp) Method

SubmitRequest() private method

private SubmitRequest ( bool isAsync ) : void
isAsync bool
return void
        private void SubmitRequest(bool isAsync)
        {
            try
            {
                _async = isAsync;

                //
                // FYI: Will do 2 attempts max as per AttemptedRecovery
                //
                Stream stream;

                while (true)
                {
                    FtpControlStream connection = _connection;

                    if (connection == null)
                    {
                        if (isAsync)
                        {
                            CreateConnectionAsync();
                            return;
                        }

                        connection = CreateConnection();
                        _connection = connection;
                    }

                    if (!isAsync)
                    {
                        if (Timeout != System.Threading.Timeout.Infinite)
                        {
                            _remainingTimeout = Timeout - (int)((DateTime.UtcNow - _startTime).TotalMilliseconds);

                            if (_remainingTimeout <= 0)
                            {
                                throw ExceptionHelper.TimeoutException;
                            }
                        }
                    }

                    if (NetEventSource.IsEnabled) NetEventSource.Info(this, "Request being submitted");

                    connection.SetSocketTimeoutOption(RemainingTimeout);

                    try
                    {
                        stream = TimedSubmitRequestHelper(isAsync);
                    }
                    catch (Exception e)
                    {
                        if (AttemptedRecovery(e))
                        {
                            if (!isAsync)
                            {
                                if (Timeout != System.Threading.Timeout.Infinite)
                                {
                                    _remainingTimeout = Timeout - (int)((DateTime.UtcNow - _startTime).TotalMilliseconds);
                                    if (_remainingTimeout <= 0)
                                    {
                                        throw;
                                    }
                                }
                            }
                            continue;
                        }
                        throw;
                    }
                    // no retry needed
                    break;
                }
            }
            catch (WebException webException)
            {
                // If this was a timeout, throw a timeout exception
                IOException ioEx = webException.InnerException as IOException;
                if (ioEx != null)
                {
                    SocketException sEx = ioEx.InnerException as SocketException;
                    if (sEx != null)
                    {
                        if (sEx.SocketErrorCode == SocketError.TimedOut)
                        {
                            SetException(new WebException(SR.net_timeout, WebExceptionStatus.Timeout));
                        }
                    }
                }

                SetException(webException);
            }
            catch (Exception exception)
            {
                SetException(exception);
            }
        }