private async Task ConnectAsyncCore(Uri uri, CancellationToken cancellationToken) { _innerWebSocket = WebSocketHandle.Create(); try { // Change internal state to 'connected' to enable the other methods if ((InternalState)Interlocked.CompareExchange(ref _state, (int)InternalState.Connected, (int)InternalState.Connecting) != InternalState.Connecting) { // Aborted/Disposed during connect. throw new ObjectDisposedException(GetType().FullName); } await _innerWebSocket.ConnectAsyncCore(uri, cancellationToken, _options).ConfigureAwait(false); } catch (Exception ex) { if (NetEventSource.Log.IsEnabled()) { NetEventSource.Exception(NetEventSource.ComponentType.WebSocket, this, "ConnectAsync", ex); } throw; } }