private void sendMessage(final CommandMessage message, final InternalConnection connection) {
    ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
    try {
      int documentPosition = message.encodeWithMetadata(bsonOutput).getFirstDocumentPosition();
      sendStartedEvent(connection, bsonOutput, message, documentPosition);

      connection.sendMessage(bsonOutput.getByteBuffers(), message.getId());
    } finally {
      bsonOutput.close();
    }
  }
 @Override
 public void executeAsync(
     final InternalConnection connection, final SingleResultCallback<T> callback) {
   long startTimeNanos = System.nanoTime();
   CommandMessage message =
       new CommandMessage(
           namespace.getFullName(),
           command,
           slaveOk,
           fieldNameValidator,
           ProtocolHelper.getMessageSettings(connection.getDescription()));
   boolean sentStartedEvent = false;
   try {
     if (LOGGER.isDebugEnabled()) {
       LOGGER.debug(
           format(
               "Asynchronously sending command {%s : %s} to database %s on connection [%s] to server %s",
               getCommandName(),
               command.values().iterator().next(),
               namespace.getDatabaseName(),
               connection.getDescription().getConnectionId(),
               connection.getDescription().getServerAddress()));
     }
     ByteBufferBsonOutput bsonOutput = new ByteBufferBsonOutput(connection);
     int documentPosition =
         ProtocolHelper.encodeMessageWithMetadata(message, bsonOutput).getFirstDocumentPosition();
     sendStartedEvent(connection, bsonOutput, message, documentPosition);
     sentStartedEvent = true;
     SingleResultCallback<ResponseBuffers> receiveCallback =
         new CommandResultCallback(callback, message, connection.getDescription(), startTimeNanos);
     connection.sendMessageAsync(
         bsonOutput.getByteBuffers(),
         message.getId(),
         new SendMessageCallback<T>(
             connection,
             bsonOutput,
             message,
             getCommandName(),
             startTimeNanos,
             commandListener,
             callback,
             receiveCallback));
   } catch (Throwable t) {
     if (sentStartedEvent) {
       sendFailedEvent(connection.getDescription(), startTimeNanos, message, t);
     }
     callback.onResult(null, t);
   }
 }