/**
   * Notification message saying that the server supports TLS so confirm the server that we want to
   * secure the connection.
   *
   * @param required true when the server indicates that TLS is required.
   */
  void startTLSReceived(boolean required) {
    if (required && config.getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled) {
      packetReader.notifyConnectionError(
          new IllegalStateException(
              "TLS required by server but not allowed by connection configuration"));
      return;
    }

    if (required && usingSSL) {
      packetReader.notifyConnectionError(
          new IllegalStateException("TLS required by server but legacy SSL already enabled"));
      return;
    }

    if ((config.getSecurityMode() == ConnectionConfiguration.SecurityMode.disabled)
        || (config.getSecurityMode() == ConnectionConfiguration.SecurityMode.legacy)) {
      // Do not secure the connection using TLS since TLS was disabled or we are using SSL.
      return;
    }

    try {
      writer.write("<starttls xmlns=\"urn:ietf:params:xml:ns:xmpp-tls\"/>");
      writer.flush();
    } catch (IOException e) {
      packetReader.notifyConnectionError(e);
    }
  }
 /**
  * Request the server that we want to start using stream compression. When using TLS then
  * negotiation of stream compression can only happen after TLS was negotiated. If TLS compression
  * is being used the stream compression should not be used.
  */
 private void requestStreamCompression() {
   try {
     writer.write("<compress xmlns='http://jabber.org/protocol/compress'>");
     writer.write("<method>zlib</method></compress>");
     writer.flush();
   } catch (IOException e) {
     packetReader.notifyConnectionError(e);
   }
 }