internal void Close(TimeSpan timeout, Microsoft.ServiceBus.Channels.TransportChannelListener channelListener)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            using (Microsoft.ServiceBus.Diagnostics.Activity activity = ServiceModelActivity.BoundOperation(this.Activity))
            {
                this.Unregister(timeoutHelper.RemainingTime(), channelListener);
            }
            lock (this.ThisLock)
            {
                if (this.openCount <= 0)
                {
                    throw Fx.AssertAndThrow("Invalid Open/Close state machine.");
                }
                Microsoft.ServiceBus.Channels.TransportManager transportManager = this;
                transportManager.openCount = transportManager.openCount - 1;
                if (this.openCount == 0)
                {
                    using (Microsoft.ServiceBus.Diagnostics.Activity activity1 = ServiceModelActivity.BoundOperation(this.Activity, true))
                    {
                        this.OnClose(timeoutHelper.RemainingTime());
                    }
                    if (this.Activity != null)
                    {
                        this.Activity.Dispose();
                    }
                }
            }
        }
        internal void Open(TimeSpan timeout, Microsoft.ServiceBus.Channels.TransportChannelListener channelListener)
        {
            TimeoutHelper timeoutHelper = new TimeoutHelper(timeout);

            if (DiagnosticUtility.ShouldUseActivity)
            {
                if (this.activity == null)
                {
                    this.activity = ServiceModelActivity.CreateActivity(true);
                }
                channelListener.ServiceModelActivity = this.Activity;
            }
            using (Microsoft.ServiceBus.Diagnostics.Activity activity = ServiceModelActivity.BoundOperation(this.Activity))
            {
                if (DiagnosticUtility.ShouldTraceInformation)
                {
                    DiagnosticTrace diagnosticTrace          = DiagnosticUtility.DiagnosticTrace;
                    string          traceCodeTransportListen = Resources.TraceCodeTransportListen;
                    object[]        absoluteUri = new object[] { channelListener.Uri.AbsoluteUri };
                    diagnosticTrace.TraceEvent(TraceEventType.Information, TraceCode.TransportListen, Microsoft.ServiceBus.SR.GetString(traceCodeTransportListen, absoluteUri), null, null, this);
                }
                this.Register(timeoutHelper.RemainingTime(), channelListener);
                try
                {
                    lock (this.ThisLock)
                    {
                        if (this.openCount == 0)
                        {
                            this.OnOpen(timeoutHelper.RemainingTime());
                        }
                        Microsoft.ServiceBus.Channels.TransportManager transportManager = this;
                        transportManager.openCount = transportManager.openCount + 1;
                    }
                }
                catch
                {
                    this.Unregister(timeoutHelper.RemainingTime(), channelListener);
                    throw;
                }
            }
        }