h2. Connection and disconnection *LDAP* is a protocol which requires the user to be connected--and eventually identified--in order to be able to send requests to the server. We maintain this connection potentially forever. What makes the *LDAP* protocol different from, say, the *HTTP* protocol, is that the connection must be issued explicitly. Let's see how we do that. h3. Opening a Connection We can open a standard or secure connection. h4. Standard Connection We can first establish a standard connection, where the data is sent and received in clear text (encoded in ASN.1 BER, but still unencrypted). This example shows the way it's done : {code:java} LdapConnection connection = new LdapNetworkConnection( "localhost", 389 ); {code} Here, we just created an unsafe connection locally, using port 389. Quite simple... h4. Secure Connection Although the *LDAPS* (*LDAP* over *SSL*) is now considered deprecated, many people continue to use it. The big advantage of not using *LDAPS* is that you don't need to set up two different listening ports (one for *LDAP* -389- and another one for *LDAPS* -636- ). The only difference with the previous example is that we have to tell the connection that it has to use *SSL*, by passing *_true{_}* as a third parameter (incidentally, passing *_false{_}* set a unsafe connection). Here is an example: {code:java} LdapConnection connection = new LdapNetworkConnection( "localhost", 636, true ); {code} h3. Keeping the Connection Open We keep the connection open for a limited period of time, defaulting to 30 seconds. This might be not long enough, so one can change this delay by calling the _setTimeOut()_ method : {code:java} LdapConnection connection = new LdapNetworkConnection( "localhost", 389 ); connection.setTimeOut( 0 ); ... connection.close(); {code} {note} Setting a value equal to or below 0 will keep the connection open forever (as long as the connection is not explicitly closed). {note} h3. Closing the Connection Once you don't need to use the connection anymore (holding a connection keeps a session open on the server, and a socket open between the client and the server), then you have to close it. This is done by calling the _close()_ method: {code:java} LdapConnection connection = new LdapNetworkConnection( "localhost", 389 ); ... connection.close(); {code}