public IAsyncResult BeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
        {
            CommunicationState communicationState;
            IAsyncResult       skippingOperationAsyncResult;

            if (timeout < TimeSpan.Zero)
            {
                throw Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("timeout", Microsoft.ServiceBus.SR.GetString(Resources.SFxTimeoutOutOfRange0, new object[0])));
            }
            lock (this.ThisLock)
            {
                this.ThrowIfDisposed();
                if (this.state != CommunicationState.Opened)
                {
                    communicationState = this.state;
                    if (this.state == CommunicationState.Created)
                    {
                        this.state = CommunicationState.Opening;
                        if (!this.semaphore.TryEnter())
                        {
                            throw new Exception(SRClient.InvalidStateMachineRefcountedCommunicationObject);
                        }
                    }
                }
                else
                {
                    skippingOperationAsyncResult = new RefcountedCommunicationObject.SkippingOperationAsyncResult(callback, state);
                    return(skippingOperationAsyncResult);
                }
            }
            if (communicationState == CommunicationState.Opening)
            {
                return(new RefcountedCommunicationObject.AlreadyOpeningAsyncResult(this.semaphore, timeout, callback, state));
            }
            bool flag = true;

            try
            {
                this.OnOpening();
                if (!this.onOpeningCalled)
                {
                    throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnOpening"), Guid.Empty, this);
                }
                IAsyncResult openAsyncResult = new RefcountedCommunicationObject.OpenAsyncResult(this, timeout, callback, state);
                flag = false;
                skippingOperationAsyncResult = openAsyncResult;
            }
            finally
            {
                if (flag)
                {
                    if (Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldTraceWarning)
                    {
                        DiagnosticTrace diagnosticTrace = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.DiagnosticTrace;
                        string          traceCodeCommunicationObjectOpenFailed = Resources.TraceCodeCommunicationObjectOpenFailed;
                        object[]        str = new object[] { this.GetCommunicationObjectType().ToString() };
                        diagnosticTrace.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectOpenFailed, Microsoft.ServiceBus.SR.GetString(traceCodeCommunicationObjectOpenFailed, str), null, null, this);
                    }
                    this.Fault();
                }
            }
            return(skippingOperationAsyncResult);
        }
        public IAsyncResult BeginClose(TimeSpan timeout, AsyncCallback callback, object state)
        {
            CommunicationState   communicationState;
            IAsyncResult         skippingOperationAsyncResult;
            ServiceModelActivity serviceModelActivity;

            if (timeout < TimeSpan.Zero)
            {
                throw Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("timeout", Microsoft.ServiceBus.SR.GetString(Resources.SFxTimeoutOutOfRange0, new object[0])));
            }
            if (!Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldUseActivity || !this.TraceOpenAndClose)
            {
                serviceModelActivity = null;
            }
            else
            {
                serviceModelActivity = this.CreateCloseActivity();
            }
            using (serviceModelActivity)
            {
                lock (this.ThisLock)
                {
                    if (this.refCount == 0)
                    {
                        throw new InvalidOperationException(SRClient.InvalidRefcountedCommunicationObject);
                    }
                    RefcountedCommunicationObject refcountedCommunicationObject = this;
                    int num  = refcountedCommunicationObject.refCount - 1;
                    int num1 = num;
                    refcountedCommunicationObject.refCount = num;
                    if (num1 <= 0)
                    {
                        communicationState = this.state;
                        if (communicationState != CommunicationState.Closed)
                        {
                            this.state = CommunicationState.Closing;
                        }
                        this.closeCalled = true;
                    }
                    else
                    {
                        skippingOperationAsyncResult = new RefcountedCommunicationObject.SkippingOperationAsyncResult(callback, state);
                        return(skippingOperationAsyncResult);
                    }
                }
                switch (communicationState)
                {
                case CommunicationState.Created:
                case CommunicationState.Opening:
                case CommunicationState.Faulted:
                {
                    this.Abort();
                    if (communicationState == CommunicationState.Faulted)
                    {
                        throw TraceUtility.ThrowHelperError(this.CreateFaultedException(), Guid.Empty, this);
                    }
                    skippingOperationAsyncResult = new RefcountedCommunicationObject.AlreadyClosedAsyncResult(callback, state);
                    return(skippingOperationAsyncResult);
                }

                case CommunicationState.Opened:
                {
                    bool flag = true;
                    try
                    {
                        this.OnClosing();
                        if (!this.onClosingCalled)
                        {
                            throw TraceUtility.ThrowHelperError(this.CreateBaseClassMethodNotCalledException("OnClosing"), Guid.Empty, this);
                        }
                        IAsyncResult closeAsyncResult = new RefcountedCommunicationObject.CloseAsyncResult(this, timeout, callback, state);
                        flag = false;
                        skippingOperationAsyncResult = closeAsyncResult;
                        return(skippingOperationAsyncResult);
                    }
                    finally
                    {
                        if (flag)
                        {
                            if (Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldTraceWarning)
                            {
                                DiagnosticTrace diagnosticTrace = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.DiagnosticTrace;
                                string          traceCodeCommunicationObjectCloseFailed = Resources.TraceCodeCommunicationObjectCloseFailed;
                                object[]        str = new object[] { this.GetCommunicationObjectType().ToString() };
                                diagnosticTrace.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectCloseFailed, Microsoft.ServiceBus.SR.GetString(traceCodeCommunicationObjectCloseFailed, str), null, null, this);
                            }
                            this.Abort();
                        }
                    }
                    break;
                }

                case CommunicationState.Closing:
                case CommunicationState.Closed:
                {
                    skippingOperationAsyncResult = new RefcountedCommunicationObject.AlreadyClosedAsyncResult(callback, state);
                    return(skippingOperationAsyncResult);
                }
                }
                throw Fx.AssertAndThrow("CommunicationObject.BeginClose: Unknown CommunicationState");
            }
            return(skippingOperationAsyncResult);
        }