private static void OnOpenComplete(IAsyncResult result)
            {
                if (result.CompletedSynchronously)
                {
                    return;
                }
                Exception exception = null;

                RefcountedCommunicationObject.OpenAsyncResult asyncState = (RefcountedCommunicationObject.OpenAsyncResult)result.AsyncState;
                try
                {
                    asyncState.HandleOpenComplete(result);
                }
                catch (Exception exception2)
                {
                    Exception exception1 = exception2;
                    if (Fx.IsFatal(exception1))
                    {
                        throw;
                    }
                    exception = exception1;
                    if (Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.ShouldTraceWarning)
                    {
                        DiagnosticTrace diagnosticTrace = Microsoft.ServiceBus.Diagnostics.DiagnosticUtility.DiagnosticTrace;
                        string          traceCodeCommunicationObjectOpenFailed = Resources.TraceCodeCommunicationObjectOpenFailed;
                        object[]        str = new object[] { asyncState.communicationObject.GetCommunicationObjectType().ToString() };
                        diagnosticTrace.TraceEvent(TraceEventType.Warning, TraceCode.CommunicationObjectOpenFailed, Microsoft.ServiceBus.SR.GetString(traceCodeCommunicationObjectOpenFailed, str), null, null, asyncState);
                    }
                    asyncState.communicationObject.Fault();
                }
                asyncState.Complete(false, exception);
            }
        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);
        }