public void cancelPingKeepAliveTimeoutTaskIfStarted() {
   if (pingKeepAliveTimeoutTask != null && pingKeepAliveTimeoutTask.getSipTimerTask() != null) {
     try {
       keepAliveSemaphore.acquire();
     } catch (InterruptedException e) {
       logger.logError("Couldn't acquire keepAliveSemaphore");
       return;
     }
     try {
       if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
         logger.logDebug(
             "~~~ cancelPingKeepAliveTimeoutTaskIfStarted for MessageChannel(key="
                 + key
                 + "), clientAddress="
                 + peerAddress
                 + ", clientPort="
                 + peerPort
                 + ", timeout="
                 + keepAliveTimeout
                 + ")");
       }
       sipStack.getTimer().cancel(pingKeepAliveTimeoutTask);
     } finally {
       keepAliveSemaphore.release();
     }
   }
 }
  public void rescheduleKeepAliveTimeout(long newKeepAliveTimeout) {
    //        long now = System.currentTimeMillis();
    //        long lastKeepAliveReceivedTimeOrNow = lastKeepAliveReceivedTime == 0 ? now :
    // lastKeepAliveReceivedTime;
    //
    //        long newScheduledTime =  lastKeepAliveReceivedTimeOrNow + newKeepAliveTimeout;

    StringBuilder methodLog = new StringBuilder();

    if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
      methodLog.append(
          "~~~ rescheduleKeepAliveTimeout for MessageChannel(key="
              + key
              + "), clientAddress="
              + peerAddress
              + ", clientPort="
              + peerPort
              + ", timeout="
              + keepAliveTimeout
              + "): newKeepAliveTimeout=");
      if (newKeepAliveTimeout == Long.MAX_VALUE) {
        methodLog.append("Long.MAX_VALUE");
      } else {
        methodLog.append(newKeepAliveTimeout);
      }
      //            methodLog.append(", lastKeepAliveReceivedTimeOrNow=");
      //            methodLog.append(lastKeepAliveReceivedTimeOrNow);
      //            methodLog.append(", newScheduledTime=");
      //            methodLog.append(newScheduledTime);
    }

    //        long delay = newScheduledTime > now ? newScheduledTime - now : 1;
    try {
      keepAliveSemaphore.acquire();
    } catch (InterruptedException e) {
      logger.logWarning("Couldn't acquire keepAliveSemaphore");
      return;
    }
    try {
      if (pingKeepAliveTimeoutTask == null) {
        pingKeepAliveTimeoutTask = new KeepAliveTimeoutTimerTask();
        if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
          methodLog.append(", scheduling pingKeepAliveTimeoutTask to execute after ");
          methodLog.append(keepAliveTimeout / 1000);
          methodLog.append(" seconds");
          logger.logDebug(methodLog.toString());
        }
        sipStack.getTimer().schedule(pingKeepAliveTimeoutTask, keepAliveTimeout);
      } else {
        if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
          logger.logDebug(
              "~~~ cancelPingKeepAliveTimeout for MessageChannel(key="
                  + key
                  + "), clientAddress="
                  + peerAddress
                  + ", clientPort="
                  + peerPort
                  + ", timeout="
                  + keepAliveTimeout
                  + ")");
        }
        sipStack.getTimer().cancel(pingKeepAliveTimeoutTask);
        pingKeepAliveTimeoutTask = new KeepAliveTimeoutTimerTask();
        if (logger.isLoggingEnabled(LogWriter.TRACE_DEBUG)) {
          methodLog.append(", scheduling pingKeepAliveTimeoutTask to execute after ");
          methodLog.append(keepAliveTimeout / 1000);
          methodLog.append(" seconds");
          logger.logDebug(methodLog.toString());
        }
        sipStack.getTimer().schedule(pingKeepAliveTimeoutTask, keepAliveTimeout);
      }
    } finally {
      keepAliveSemaphore.release();
    }
  }