protected virtual void Dispose(bool disposing)
 {
     if (disposing)
     {
         if (!_disposed)
         {
             try
             {
                 if (_cursorId != 0)
                 {
                     KillCursorAsync(_cursorId).GetAwaiter().GetResult();
                 }
             }
             catch
             {
                 // ignore exceptions
             }
             if (_connectionSource != null)
             {
                 _connectionSource.Dispose();
             }
         }
     }
     _disposed = true;
 }
        // constructors
        public Cursor(
            IConnectionSource connectionSource,
            CollectionNamespace collectionNamespace,
            BsonDocument query,
            IReadOnlyList <TDocument> firstBatch,
            long cursorId,
            int batchSize,
            int limit,
            IBsonSerializer <TDocument> serializer,
            MessageEncoderSettings messageEncoderSettings,
            TimeSpan timeout,
            CancellationToken cancellationToken)
        {
            _connectionSource    = Ensure.IsNotNull(connectionSource, "connectionSource");
            _collectionNamespace = Ensure.IsNotNull(collectionNamespace, "collectionNamespace");
            _query                  = Ensure.IsNotNull(query, "query");
            _firstBatch             = Ensure.IsNotNull(firstBatch, "firstBatch");
            _cursorId               = cursorId;
            _batchSize              = Ensure.IsGreaterThanOrEqualToZero(batchSize, "batchSize");
            _limit                  = Ensure.IsGreaterThanOrEqualToZero(limit, "limit");
            _serializer             = Ensure.IsNotNull(serializer, "serializer");
            _messageEncoderSettings = messageEncoderSettings;
            _timeout                = timeout;
            _cancellationToken      = cancellationToken;

            if (_limit == 0)
            {
                _limit = int.MaxValue;
            }
            if (_firstBatch.Count > _limit)
            {
                _firstBatch = _firstBatch.Take(_limit).ToList();
            }
            _count = _firstBatch.Count;

            // if we aren't going to need the connection source we can go ahead and Dispose it now
            if (_cursorId == 0)
            {
                _connectionSource.Dispose();
                _connectionSource = null;
            }
        }