@Override
  public void run() {
    try {
      logger.info("Send OPN message to the PS");
      sendRequest(MessageFactory.getInstance().create(OPCode.OPN, new Properties()));

      // wait for CAT
      final COPSMsg recvMsg = readMessage();

      switch (recvMsg.getHeader().getOpCode()) {
        case CC:
          final COPSClientCloseMsg closeMsg = (COPSClientCloseMsg) recvMsg;
          logger.info("PS requested Client-Close" + closeMsg.getError().getDescription());
          // send a CC message and close the socket
          disconnect();
          break;
        case CAT:
          logger.info("received Client-Accept from PS");
          final COPSClientAcceptMsg acceptMsg = (COPSClientAcceptMsg) recvMsg;
          // Support
          if (acceptMsg.getIntegrity() != null) {
            throw new COPSPepException("Unsupported object (Integrity)");
          }

          // Mandatory KATimer
          final COPSKATimer kt = acceptMsg.getKATimer();
          if (kt == null) throw new COPSPepException("Mandatory COPS object missing (KA Timer)");
          final short kaTimeVal = kt.getTimerVal();

          // ACTimer
          final COPSAcctTimer at = acceptMsg.getAcctTimer();
          short acctTimer = 0;
          if (at != null) acctTimer = at.getTimerVal();

          logger.info("Send a REQ message to the PS");
          final Properties prop = new Properties();
          final COPSMsg reqMsg = MessageFactory.getInstance().create(OPCode.REQ, prop);
          final COPSHandle handle = ((COPSReqMsg) reqMsg).getClientHandle();
          sendRequest(reqMsg);

          // Create the connection manager
          final PcmmCmtsConnection conn = new PcmmCmtsConnection(CLIENT_TYPE, getSocket(), config);
          conn.addRequestState(handle, new CmtsDataProcessor());
          conn.setKaTimer(kaTimeVal);
          conn.setAcctTimer(acctTimer);

          logger.info(getClass().getName() + " Thread(conn).start");
          thread = new Thread(conn);
          thread.start();
          break;
        default:
          throw new COPSPepException(
              "Message not expected. Closing connection for " + getSocket().toString());
      }
    } catch (Exception e) {
      logger.error(e.getMessage());
    }
  }
 public void stop() {
   if (thread != null && thread.isAlive()) thread.interrupt();
 }