private static void ProcessUpgradeRequest(FramingConnection connection, ServerSessionDecoder decoder)
        {
            StreamUpgradeAcceptor upgradeAcceptor = connection.StreamUpgradeAcceptor;

            if (upgradeAcceptor == null)
            {
                // TODO: SendFault
                //SendFault(FramingEncodingString.UpgradeInvalidFault, ref timeoutHelper);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new ProtocolException(SR.Format(SR.UpgradeRequestToNonupgradableService, decoder.Upgrade)));
            }

            if (!upgradeAcceptor.CanUpgrade(decoder.Upgrade))
            {
                // TODO: SendFault
                //SendFault(FramingEncodingString.UpgradeInvalidFault, ref timeoutHelper);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
                          new ProtocolException(SR.Format(SR.UpgradeProtocolNotSupported, decoder.Upgrade)));
            }
        }
        public Microsoft.ServiceBus.Channels.IConnection CompletePreamble(TimeSpan timeout)
        {
            Microsoft.ServiceBus.Common.TimeoutHelper timeoutHelper = new Microsoft.ServiceBus.Common.TimeoutHelper(timeout);
            if (!this.transportSettings.MessageEncoderFactory.Encoder.IsContentTypeSupported(this.decoder.ContentType))
            {
                this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/ContentTypeInvalid", ref timeoutHelper);
                ExceptionUtility exceptionUtility    = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility;
                string           contentTypeMismatch = Resources.ContentTypeMismatch;
                object[]         contentType         = new object[] { this.decoder.ContentType, this.transportSettings.MessageEncoderFactory.Encoder.ContentType };
                throw exceptionUtility.ThrowHelperError(new ProtocolException(Microsoft.ServiceBus.SR.GetString(contentTypeMismatch, contentType)));
            }
            StreamUpgradeAcceptor streamUpgradeAcceptor = null;
            StreamUpgradeProvider upgrade = this.transportSettings.Upgrade;

            if (upgrade != null)
            {
                streamUpgradeAcceptor = upgrade.CreateUpgradeAcceptor();
            }
            Microsoft.ServiceBus.Channels.IConnection connection = base.Connection;
            while (true)
            {
                if (this.size == 0)
                {
                    this.offset = 0;
                    this.size   = connection.Read(this.connectionBuffer, 0, (int)this.connectionBuffer.Length, timeoutHelper.RemainingTime());
                    if (this.size == 0)
                    {
                        break;
                    }
                }
                do
                {
                    int num = this.decoder.Decode(this.connectionBuffer, this.offset, this.size);
                    if (num > 0)
                    {
                        Microsoft.ServiceBus.Channels.ServerSingletonPreambleConnectionReader serverSingletonPreambleConnectionReader = this;
                        serverSingletonPreambleConnectionReader.offset = serverSingletonPreambleConnectionReader.offset + num;
                        Microsoft.ServiceBus.Channels.ServerSingletonPreambleConnectionReader serverSingletonPreambleConnectionReader1 = this;
                        serverSingletonPreambleConnectionReader1.size = serverSingletonPreambleConnectionReader1.size - num;
                    }
                    switch (this.decoder.CurrentState)
                    {
                    case Microsoft.ServiceBus.Channels.ServerSingletonDecoder.State.UpgradeRequest:
                    {
                        if (streamUpgradeAcceptor == null)
                        {
                            this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper);
                            ExceptionUtility exceptionUtility1 = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility;
                            string           upgradeRequestToNonupgradableService = Resources.UpgradeRequestToNonupgradableService;
                            object[]         objArray = new object[] { this.decoder.Upgrade };
                            throw exceptionUtility1.ThrowHelperError(new ProtocolException(Microsoft.ServiceBus.SR.GetString(upgradeRequestToNonupgradableService, objArray)));
                        }
                        if (!streamUpgradeAcceptor.CanUpgrade(this.decoder.Upgrade))
                        {
                            this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper);
                            ExceptionUtility exceptionUtility2           = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility;
                            string           upgradeProtocolNotSupported = Resources.UpgradeProtocolNotSupported;
                            object[]         upgrade1 = new object[] { this.decoder.Upgrade };
                            throw exceptionUtility2.ThrowHelperError(new ProtocolException(Microsoft.ServiceBus.SR.GetString(upgradeProtocolNotSupported, upgrade1)));
                        }
                        connection.Write(Microsoft.ServiceBus.Channels.ServerSingletonEncoder.UpgradeResponseBytes, 0, (int)Microsoft.ServiceBus.Channels.ServerSingletonEncoder.UpgradeResponseBytes.Length, true, timeoutHelper.RemainingTime());
                        Microsoft.ServiceBus.Channels.IConnection preReadConnection = connection;
                        if (this.size > 0)
                        {
                            preReadConnection = new Microsoft.ServiceBus.Channels.PreReadConnection(preReadConnection, this.connectionBuffer, this.offset, this.size);
                        }
                        try
                        {
                            connection            = Microsoft.ServiceBus.Channels.InitialServerConnectionReader.UpgradeConnection(preReadConnection, streamUpgradeAcceptor, this.transportSettings);
                            this.connectionBuffer = connection.AsyncReadBuffer;
                            continue;
                        }
                        catch (Exception exception1)
                        {
                            Exception exception = exception1;
                            if (Fx.IsFatal(exception))
                            {
                                throw;
                            }
                            this.WriteAuditFailure(streamUpgradeAcceptor as StreamSecurityUpgradeAcceptor, exception);
                            throw;
                        }
                        break;
                    }

                    case Microsoft.ServiceBus.Channels.ServerSingletonDecoder.State.Start:
                    {
                        this.SetupSecurityIfNecessary(streamUpgradeAcceptor);
                        connection.Write(Microsoft.ServiceBus.Channels.ServerSessionEncoder.AckResponseBytes, 0, (int)Microsoft.ServiceBus.Channels.ServerSessionEncoder.AckResponseBytes.Length, true, timeoutHelper.RemainingTime());
                        return(connection);
                    }

                    default:
                    {
                        continue;
                    }
                    }
                }while (this.size != 0);
            }
            throw Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException());
        }