/**
  * 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
  }
  /**
   * Method description
   *
   * @param service
   */
  @TODO(
      note =
          "Do something if service with the same unique ID is already started, "
              + "possibly kill the old one...")
  public void serviceStarted(final IO service) {

    // synchronized(services) {
    String id = getUniqueId(service);

    if (log.isLoggable(Level.FINER)) {
      log.log(Level.FINER, "[[{0}]] Connection started: {1}", new Object[] {getName(), service});
    }

    IO serv = services.get(id);

    if (serv != null) {
      if (serv == service) {
        log.log(
            Level.WARNING,
            "{0}: That would explain a lot, adding the same service twice, ID: {1}",
            new Object[] {getName(), serv});
      } else {

        // Is it at all possible to happen???
        // let's log it for now....
        log.log(
            Level.FINE,
            "{0}: Attempt to add different service with the same ID: {1}",
            new Object[] {getName(), service});

        // And stop the old service....
        serv.stop();
      }
    }
    services.put(id, service);
    ++services_size;

    // }
  }