/**
  * Method description
  *
  * @param ios
  * @param data
  */
 protected void writeRawData(IO ios, String data) {
   try {
     ios.writeRawData(data);
     SocketThread.addSocketService(ios);
   } catch (Exception e) {
     log.log(Level.WARNING, ios + "Exception during writing data: " + data, e);
     try {
       ios.stop();
     } catch (Exception e1) {
       log.log(Level.WARNING, ios + "Exception stopping XMPPIOService: ", e1);
     } // end of try-catch
   }
 }
  /**
   * Method description
   *
   * @param ios
   * @param p
   * @return a value of <code>boolean</code>
   */
  public boolean writePacketToSocket(IO ios, Packet p) {
    if (ios != null) {
      if (log.isLoggable(Level.FINER) && !log.isLoggable(Level.FINEST)) {
        log.log(
            Level.FINER,
            "{0}, Processing packet: {1}, type: {2}",
            new Object[] {ios, p.getElemName(), p.getType()});
      }
      if (log.isLoggable(Level.FINEST)) {
        log.log(Level.FINEST, "{0}, Writing packet: {1}", new Object[] {ios, p});
      }

      // synchronized (ios) {
      ios.addPacketToSend(p);
      if (ios.writeInProgress.tryLock()) {
        try {
          ios.processWaitingPackets();
          SocketThread.addSocketService(ios);

          return true;
        } catch (Exception e) {
          log.log(Level.WARNING, ios + "Exception during writing packets: ", e);
          try {
            ios.stop();
          } catch (Exception e1) {
            log.log(Level.WARNING, ios + "Exception stopping XMPPIOService: ", e1);
          } // end of try-catch
        } finally {
          ios.writeInProgress.unlock();
        }
      }

    } else {
      if (log.isLoggable(Level.FINE)) {
        log.log(
            Level.FINE,
            "Can''t find service for packet: <{0}> {1}, service id: {2}",
            new Object[] {p.getElemName(), p.getTo(), getServiceId(p)});
      }
    } // end of if (ios != null) else

    return false;
  }
  /**
   * Method description
   *
   * @param serv
   * @param packets
   */
  public void writePacketsToSocket(IO serv, Queue<Packet> packets) {
    if (serv != null) {

      // synchronized (serv) {
      if ((packets != null) && (packets.size() > 0)) {
        Packet p = null;

        while ((p = packets.poll()) != null) {
          if (log.isLoggable(Level.FINER) && !log.isLoggable(Level.FINEST)) {
            log.log(
                Level.FINER,
                "{0}, Processing packet: {1}, type: {2}",
                new Object[] {serv, p.getElemName(), p.getType()});
          }
          if (log.isLoggable(Level.FINEST)) {
            log.log(Level.FINEST, "{0}, Writing packet: {1}", new Object[] {serv, p});
          }
          serv.addPacketToSend(p);
        } // end of for ()
        try {
          serv.processWaitingPackets();
          SocketThread.addSocketService(serv);
        } catch (Exception e) {
          log.log(Level.WARNING, serv + "Exception during writing packets: ", e);
          try {
            serv.stop();
          } catch (Exception e1) {
            log.log(Level.WARNING, serv + "Exception stopping XMPPIOService: ", e1);
          } // end of try-catch
        } // end of try-catch
      }

      // }
    } else {
      if (log.isLoggable(Level.FINE)) {
        log.log(Level.FINE, "Can't find service for packets: [{0}] ", packets);
      }
    } // end of if (ios != null) else
  }
    @Override
    public void accept(SocketChannel sc) {
      String cid = "" + port_props.get("local-hostname") + "@" + port_props.get("remote-hostname");

      if (log.isLoggable(Level.FINEST)) {
        log.log(
            Level.FINEST,
            "Accept called for service: {0}, port_props: {1}",
            new Object[] {cid, port_props});
      }

      IO serv = getXMPPIOServiceInstance();
      serv.setBufferLimit(net_buffer_limit);

      ((XMPPDomBuilderHandler) serv.getSessionData().get(DOM_HANDLER))
          .setElementsLimit(elements_number_limit);

      serv.setIOServiceListener(ConnectionManager.this);
      serv.setSessionData(port_props);
      try {
        serv.accept(sc);
        if (getSocketType() == SocketType.ssl) {
          serv.startSSL(false, false, false);
        } // end of if (socket == SocketType.ssl)
        serviceStarted(serv);
        SocketThread.addSocketService(serv);
      } catch (Exception e) {
        if (getConnectionType() == ConnectionType.connect) {

          // Accept side for component service is not ready yet?
          // Let's wait for a few secs and try again.
          if (log.isLoggable(Level.FINEST)) {
            log.log(
                Level.FINEST,
                "Problem reconnecting the service: {0}, port_props: {1}, exception: {2}",
                new Object[] {serv, port_props, e});
          }
          updateConnectionDetails(port_props);

          boolean reconnect = false;
          Integer reconnects = (Integer) port_props.get(MAX_RECONNECTS_PROP_KEY);

          if (reconnects != null) {
            int recon = reconnects.intValue();

            if (recon != 0) {
              port_props.put(MAX_RECONNECTS_PROP_KEY, (--recon));
              reconnect = true;
            } // end of if (recon != 0)
          }
          if (reconnect) {
            reconnectService(port_props, connectionDelay);
          } else {
            reconnectionFailed(port_props);
          }
        } else {

          // Ignore
        }

        //      } catch (Exception e) {
        //        if (log.isLoggable(Level.FINEST)) {
        //          log.log(Level.FINEST, "Can not accept connection cid: " + cid, e);
        //        }
        //        log.log(Level.WARNING, "Can not accept connection.", e);
        //        serv.stop();
      } // end of try-catch
    }