/// <summary>
        /// Opens this connection so it starts receiving events from the server.
        /// This results in a long-standing call to EWS.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown when Open is called while connected.</exception>
        public void Open()
        {
            lock (this.lockObject)
            {
                this.ThrowIfDisposed();

                this.ValidateConnectionState(false, Strings.CannotCallConnectDuringLiveConnection);

                if (this.subscriptions.Count == 0)
                {
                    throw new ServiceLocalException(Strings.NoSubscriptionsOnConnection);
                }

                this.hasFiredConnectionCompleted = false;

                this.currentHangingRequest = new GetStreamingEventsRequest(
                    this.session,
                    this.HandleServiceResponseObject,
                    this.subscriptions.Keys,
                    this.connectionTimeout);

                this.currentHangingRequest.OnDisconnect += new HangingServiceRequestBase.HangingRequestDisconnectHandler(this.OnRequestDisconnect);

                this.currentHangingRequest.InternalExecute();
            }
        }
        /// <summary>
        /// Internal helper method called when the request disconnects.
        /// </summary>
        /// <param name="ex">The exception that caused the disconnection. May be null.</param>
        private void InternalOnDisconnect(Exception ex)
        {
            this.currentHangingRequest = null;

            if (this.OnDisconnect != null)
            {
                this.OnDisconnect(this, new SubscriptionErrorEventArgs(null, ex));
            }
        }
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <param name="suppressFinalizer">Value indicating whether to suppress the garbage collector's finalizer..</param>
        private void Dispose(bool suppressFinalizer)
        {
            if (suppressFinalizer)
            {
                GC.SuppressFinalize(this);
            }

            lock (this.lockObject)
            {
                if (!this.isDisposed)
                {
                    if (this.currentHangingRequest != null)
                    {
                        this.currentHangingRequest = null;
                    }

                    this.subscriptions = null;
                    this.session       = null;

                    this.isDisposed = true;
                }
            }
        }
        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        /// <param name="suppressFinalizer">Value indicating whether to suppress the garbage collector's finalizer..</param>
        private void Dispose(bool suppressFinalizer)
        {
            if (suppressFinalizer)
            {
                GC.SuppressFinalize(this);
            }

            lock (this.lockObject)
            {
                if (!this.isDisposed)
                {
                    if (this.currentHangingRequest != null)
                    {
                        this.currentHangingRequest = null;
                    }

                    this.subscriptions = null;
                    this.session = null;

                    this.isDisposed = true;
                }
            }
        }
        /// <summary>
        /// Internal helper method called when the request disconnects.
        /// </summary>
        /// <param name="ex">The exception that caused the disconnection. May be null.</param>
        private void InternalOnDisconnect(Exception ex)
        {
            this.currentHangingRequest = null;

            if (this.OnDisconnect != null)
            {
                this.OnDisconnect(this, new SubscriptionErrorEventArgs(null, ex));
            }
        }
        /// <summary>
        /// Opens this connection so it starts receiving events from the server.
        /// This results in a long-standing call to EWS.
        /// </summary>
        /// <exception cref="InvalidOperationException">Thrown when Open is called while connected.</exception>
        public void Open()
        {
            lock (this.lockObject)
            {
                this.ThrowIfDisposed();

                this.ValidateConnectionState(false, Strings.CannotCallConnectDuringLiveConnection);

                if (this.subscriptions.Count == 0)
                {
                    throw new ServiceLocalException(Strings.NoSubscriptionsOnConnection);
                }

                this.currentHangingRequest = new GetStreamingEventsRequest(
                    this.session,
                    this.HandleServiceResponseObject,
                    this.subscriptions.Keys,
                    this.connectionTimeout);

                this.currentHangingRequest.OnDisconnect += new HangingServiceRequestBase.HangingRequestDisconnectHandler(this.OnRequestDisconnect);

                this.currentHangingRequest.InternalExecute();
            }
        }