internal void Connect(
            bool slaveOk
            )
        {
            if (state != MongoServerState.Disconnected)
            {
                var message = string.Format("MongoServerInstance.Connect called when state is: {0}", state);
                throw new InvalidOperationException(message);
            }

            State            = MongoServerState.Connecting;
            connectException = null;
            try {
                endPoint = address.ToIPEndPoint(server.Settings.AddressFamily);

                var connectionPool = new MongoConnectionPool(this);
                try {
                    var connection = connectionPool.AcquireConnection(null);
                    try {
                        try {
                            var isMasterCommand = new CommandDocument("ismaster", 1);
                            isMasterResult = connection.RunCommand("admin.$cmd", QueryFlags.SlaveOk, isMasterCommand);
                        } catch (MongoCommandException ex) {
                            isMasterResult = ex.CommandResult;
                            throw;
                        }

                        isPrimary   = isMasterResult.Response["ismaster", false].ToBoolean();
                        isSecondary = isMasterResult.Response["secondary", false].ToBoolean();
                        isPassive   = isMasterResult.Response["passive", false].ToBoolean();
                        isArbiter   = isMasterResult.Response["arbiterOnly", false].ToBoolean();
                        if (!isPrimary && !slaveOk)
                        {
                            throw new MongoConnectionException("Server is not a primary and SlaveOk is false");
                        }

                        maxDocumentSize  = isMasterResult.Response["maxBsonObjectSize", MongoDefaults.MaxDocumentSize].ToInt32();
                        maxMessageLength = Math.Max(MongoDefaults.MaxMessageLength, maxDocumentSize + 1024); // derived from maxDocumentSize
                    } finally {
                        connectionPool.ReleaseConnection(connection);
                    }
                } catch {
                    connectionPool.Close();
                    throw;
                }

                State = MongoServerState.Connected;
                this.connectionPool = connectionPool;
            } catch (Exception ex) {
                State            = MongoServerState.Disconnected;
                connectException = ex;
                throw;
            }
        }
 internal void Disconnect()
 {
     if (state != MongoServerState.Disconnected)
     {
         try {
             connectionPool.Close();
             connectionPool = null;
         } finally {
             State = MongoServerState.Disconnected;
         }
     }
 }
 internal void Disconnect()
 {
     if (state != MongoServerState.Disconnected)
     {
         try {
             // if we fail during Connect the connectionPool field will still be null
             if (connectionPool != null)
             {
                 connectionPool.Close();
                 connectionPool = null;
             }
         } finally {
             State = MongoServerState.Disconnected;
         }
     }
 }