public IConnection CompletePreamble(TimeSpan timeout)
        {
            int           num;
            TimeoutHelper timeoutHelper = new 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);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("ContentTypeMismatch", new object[] { this.decoder.ContentType, this.transportSettings.MessageEncoderFactory.Encoder.ContentType })));
            }
            StreamUpgradeAcceptor upgradeAcceptor         = null;
            StreamUpgradeProvider upgrade                 = this.transportSettings.Upgrade;
            IStreamUpgradeChannelBindingProvider property = null;

            if (upgrade != null)
            {
                property        = upgrade.GetProperty <IStreamUpgradeChannelBindingProvider>();
                upgradeAcceptor = upgrade.CreateUpgradeAcceptor();
            }
            IConnection connection = base.Connection;

Label_00B1:
            if (this.size == 0)
            {
                this.offset = 0;
                this.size   = connection.Read(this.connectionBuffer, 0, this.connectionBuffer.Length, timeoutHelper.RemainingTime());
                if (this.size == 0)
                {
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException());
                }
            }
Label_0101:
            num = this.decoder.Decode(this.connectionBuffer, this.offset, this.size);
            if (num > 0)
            {
                this.offset += num;
                this.size   -= num;
            }
            switch (this.decoder.CurrentState)
            {
            case ServerSingletonDecoder.State.UpgradeRequest:
            {
                if (upgradeAcceptor == null)
                {
                    this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("UpgradeRequestToNonupgradableService", new object[] { this.decoder.Upgrade })));
                }
                if (!upgradeAcceptor.CanUpgrade(this.decoder.Upgrade))
                {
                    this.SendFault("http://schemas.microsoft.com/ws/2006/05/framing/faults/UpgradeInvalid", ref timeoutHelper);
                    throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(System.ServiceModel.SR.GetString("UpgradeProtocolNotSupported", new object[] { this.decoder.Upgrade })));
                }
                connection.Write(ServerSingletonEncoder.UpgradeResponseBytes, 0, ServerSingletonEncoder.UpgradeResponseBytes.Length, true, timeoutHelper.RemainingTime());
                IConnection innerConnection = connection;
                if (this.size > 0)
                {
                    innerConnection = new PreReadConnection(innerConnection, this.connectionBuffer, this.offset, this.size);
                }
                try
                {
                    connection            = InitialServerConnectionReader.UpgradeConnection(innerConnection, upgradeAcceptor, this.transportSettings);
                    this.connectionBuffer = connection.AsyncReadBuffer;
                    if ((property != null) && property.IsChannelBindingSupportEnabled)
                    {
                        this.channelBindingToken = property.GetChannelBinding(upgradeAcceptor, ChannelBindingKind.Endpoint);
                    }
                    goto Label_02C0;
                }
                catch (Exception exception)
                {
                    if (Fx.IsFatal(exception))
                    {
                        throw;
                    }
                    this.WriteAuditFailure(upgradeAcceptor as StreamSecurityUpgradeAcceptor, exception);
                    throw;
                }
                break;
            }

            case ServerSingletonDecoder.State.Start:
                break;

            default:
                goto Label_02C0;
            }
            this.SetupSecurityIfNecessary(upgradeAcceptor);
            connection.Write(ServerSessionEncoder.AckResponseBytes, 0, ServerSessionEncoder.AckResponseBytes.Length, true, timeoutHelper.RemainingTime());
            return(connection);

Label_02C0:
            if (this.size != 0)
            {
                goto Label_0101;
            }
            goto Label_00B1;
        }
            protected override void OnOpen(TimeSpan timeout)
            {
                bool flag = false;
                try
                {
                    TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);
                    this.ValidateContentType(ref timeoutHelper);
                Label_0017:
                    if (this.size == 0)
                    {
                        this.offset = 0;
                        this.size = base.Connection.Read(this.connectionBuffer, 0, this.connectionBuffer.Length, timeoutHelper.RemainingTime());
                        if (this.size == 0)
                        {
                            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(this.decoder.CreatePrematureEOFException());
                        }
                    }
                Label_006B:
                    this.DecodeBytes();
                    switch (this.decoder.CurrentState)
                    {
                        case ServerSessionDecoder.State.UpgradeRequest:
                        {
                            this.ProcessUpgradeRequest(ref timeoutHelper);
                            base.Connection.Write(ServerSessionEncoder.UpgradeResponseBytes, 0, ServerSessionEncoder.UpgradeResponseBytes.Length, true, timeoutHelper.RemainingTime());
                            IConnection innerConnection = base.Connection;
                            if (this.size > 0)
                            {
                                innerConnection = new PreReadConnection(innerConnection, this.connectionBuffer, this.offset, this.size);
                            }
                            try
                            {
                                base.Connection = InitialServerConnectionReader.UpgradeConnection(innerConnection, this.upgradeAcceptor, this);
                                if ((this.channelBindingProvider != null) && this.channelBindingProvider.IsChannelBindingSupportEnabled)
                                {
                                    base.SetChannelBinding(this.channelBindingProvider.GetChannelBinding(this.upgradeAcceptor, ChannelBindingKind.Endpoint));
                                }
                                this.connectionBuffer = base.Connection.AsyncReadBuffer;
                                goto Label_018C;
                            }
                            catch (Exception exception)
                            {
                                if (Fx.IsFatal(exception))
                                {
                                    throw;
                                }
                                this.WriteAuditFailure(this.upgradeAcceptor as StreamSecurityUpgradeAcceptor, exception);
                                throw;
                            }
                            break;
                        }
                        case ServerSessionDecoder.State.Start:
                            break;

                        default:
                            goto Label_018C;
                    }
                    this.SetupSecurityIfNecessary();
                    base.Connection.Write(ServerSessionEncoder.AckResponseBytes, 0, ServerSessionEncoder.AckResponseBytes.Length, true, timeoutHelper.RemainingTime());
                    this.SetupSessionReader();
                    flag = true;
                    return;
                Label_018C:
                    if (this.size != 0)
                    {
                        goto Label_006B;
                    }
                    goto Label_0017;
                }
                finally
                {
                    if (!flag)
                    {
                        base.Connection.Abort();
                    }
                }
            }