connect method

Implementation

Future<ConnectionManager> connect() async {
  try {
    if (isClosed()) {
      _socket = await (_connection.isSSL
          ? SecureSocket.connect(_connection.host, _connection.port,
              onBadCertificate: _connection.badCertHandler,
              context: _context,
              timeout: _connectionTimeout)
          : Socket.connect(_connection.host, _connection.port,
              timeout: _connectionTimeout));

      _socket.transform(createLdapTransformer()).listen(
          (m) => _handleLDAPMessage(m),
          onError: _ldapListenerOnError,
          onDone: _ldapListenerOnDone);

      _isClosed = false;
    }

    return this;
  } on SocketException catch (e) {
    // For known error conditions (e.g. when the socket cannot be created)
    // throw a more meaningful exception. Otherwise, just rethrow it.

    if (e.osError != null) {
      if (e.osError?.errorCode == 61) {
        // errorCode 61 = 'Connection refused'
        throw LdapSocketRefusedException(
            e, _connection.host, _connection.port);
      }
      if (e.osError?.errorCode == 8) {
        // errorCode 8 = 'nodename nor servname provided, or not known'
        throw LdapSocketServerNotFoundException(e, _connection.host);
      }
    }
    rethrow;
  } catch (e) {
    loggerConnection.severe('Exception on connect', e);
    rethrow;
  }
}