/**
   * 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
  }