public void Dispose()
        {
            Socket socket;
            Stream stream;

            lock (_syncRoot)
            {
                if (_disposed)
                {
                    return;
                }
                if (_state != MemcachedConnectionState.Faulted)
                {
                    _state = MemcachedConnectionState.Closed;
                }
                socket    = _socket;
                _socket   = null;
                stream    = _stream;
                _stream   = null;
                _isocket  = null;
                _reader   = null;
                _disposed = true;
            }
            try
            {
                socket.Dispose();
            }
            finally
            {
                if (stream != null)
                {
                    stream.Dispose();
                }
            }
        }
        public async Task Open(TimeSpan timeout)
        {
            if (_state != MemcachedConnectionState.UnOpened)
            {
                throw new InvalidOperationException("This connection has already been opened.");
            }

            Task connectTask;

            lock (_syncRoot)
            {
                try
                {
                    connectTask = _socket.ConnectAsync(_endPoint).TimeoutAfter(timeout);
                    _state      = MemcachedConnectionState.Opening;
                }
                catch
                {
                    SetFaulted();
                    throw;
                }
            }

            try
            {
                await connectTask;
            }
            catch
            {
                lock (_syncRoot) SetFaulted();
                throw;
            }
            lock (_syncRoot)
            {
                _stream  = new NetworkStream(_socket);
                _reader  = new MemcachedResponseReader(_stream, Encoding.UTF8, _receiveTimeout);
                _isocket = new MemcachedSocket(_socket);
                _state   = MemcachedConnectionState.Open;
            }
        }
 private void SetFaulted()
 {
     _state = MemcachedConnectionState.Faulted;
     Dispose();
 }