private void connectUsingConfiguration(ConnectionConfiguration config) throws XMPPException {
   String host = config.getHost();
   int port = config.getPort();
   try {
     if (config.getSocketFactory() == null) {
       this.socket = new Socket(host, port);
     } else {
       this.socket = config.getSocketFactory().createSocket(host, port);
     }
     if (ConnectionConfiguration.SecurityMode.legacy == config.getSecurityMode()) {
       try {
         enableEncryption(false);
       } catch (Exception e) {
         String errorMessage =
             "Could not enable SSL encryption while connecting to " + host + ":" + port + ".";
         throw new XMPPException(
             errorMessage,
             new XMPPError(XMPPError.Condition.remote_server_error, errorMessage),
             e);
       }
       usingSSL = true;
     }
   } catch (UnknownHostException uhe) {
     String errorMessage = "Could not connect to " + host + ":" + port + ".";
     throw new XMPPException(
         errorMessage,
         new XMPPError(XMPPError.Condition.remote_server_timeout, errorMessage),
         uhe);
   } catch (IOException ioe) {
     String errorMessage = "XMPPError connecting to " + host + ":" + port + ".";
     throw new XMPPException(
         errorMessage, new XMPPError(XMPPError.Condition.remote_server_error, errorMessage), ioe);
   }
   initConnection();
 }
  /**
   * The server has indicated that TLS negotiation can start. We now need to secure the existing
   * plain connection and perform a handshake. This method won't return until the connection has
   * finished the handshake or an error occured while securing the connection.
   *
   * @throws Exception if an exception occurs.
   */
  void proceedTLSReceived() throws Exception {
    enableEncryption(true);
    // Initialize the reader and writer with the new secured version
    initReaderAndWriter();
    // Proceed to do the handshake
    ((SSLSocket) socket).startHandshake();
    // if (((SSLSocket) socket).getWantClientAuth()) {
    //    System.err.println("Connection wants client auth");
    // }
    // else if (((SSLSocket) socket).getNeedClientAuth()) {
    //    System.err.println("Connection needs client auth");
    // }
    // else {
    //    System.err.println("Connection does not require client auth");
    // }
    // Set that TLS was successful
    usingTLS = true;

    // Set the new  writer to use
    packetWriter.setWriter(writer);
    // Send a new opening stream to the server
    packetWriter.openStream();
  }