void discoverIfNeeded() throws java.io.IOException, PduException {
    uk.co.westhawk.snmp.beans.UsmDiscoveryBean discBean = null;
    boolean isNeeded = false;

    TimeWindow tWindow = TimeWindow.getCurrent();
    String engineId = tWindow.getSnmpEngineId(hostAddr, hostPort);
    if (engineId == null) {
      isNeeded = true;
      discBean = new uk.co.westhawk.snmp.beans.UsmDiscoveryBean(hostAddr, hostPort, typeSocket);
    }

    if (isUseAuthentication()) {
      if (isNeeded) {
        discBean.setAuthenticationDetails(
            userName, userAuthenticationPassword, authenticationProtocol);
      } else if (tWindow.isTimeLineKnown(engineId) == false) {
        isNeeded = true;
        discBean = new uk.co.westhawk.snmp.beans.UsmDiscoveryBean(hostAddr, hostPort, typeSocket);
        discBean.setAuthenticationDetails(
            userName, userAuthenticationPassword, authenticationProtocol);
      }

      if (isNeeded && isUsePrivacy()) {
        discBean.setPrivacyDetails(userPrivacyPassword);
      }
    }

    if (isNeeded) {
      discBean.startDiscovery();
    }
  }
  /**
   * Constructor.
   *
   * @param host The host to which the Pdu will send
   * @param port The port where the SNMP server will be
   * @param typeSocketA The type of socket to use.
   * @see AbstractSnmpContext#AbstractSnmpContext(String, int, String)
   * @see SnmpContextBasisFace#STANDARD_SOCKET
   * @see SnmpContextBasisFace#NETSCAPE_SOCKET
   * @see SnmpContextBasisFace#KVM_SOCKET
   */
  public SnmpContextv3(String host, int port, String typeSocketA) throws java.io.IOException {
    super(host, port, typeSocketA);

    if (TimeWindow.getCurrent() == null) {
      TimeWindow timew = new TimeWindow();
    }
    if (usmAgent == null) {
      usmAgent = createUsmAgent();
    }
  }
  /**
   * Encodes a discovery pdu packet. This methods encodes without checking is the discovery
   * paramters are all known.
   */
  public byte[] encodeDiscoveryPacket(
      byte msg_type, int rId, int errstat, int errind, Enumeration ve)
      throws java.io.IOException, EncodingException {
    String engineId = "";
    TimeWindow tWindow = TimeWindow.getCurrent();
    if (tWindow.isSnmpEngineIdKnown(hostAddr, hostPort) == true) {
      engineId = tWindow.getSnmpEngineId(hostAddr, hostPort);
    }
    TimeWindowNode node = new TimeWindowNode(engineId, 0, 0);

    return actualEncodePacket(msg_type, rId, errstat, errind, ve, node);
  }
  /**
   * Encodes a pdu packet. If the stack is authoritative, the timeline details are retrieved from
   * the usmAgent. If not, this methods first checks if all the discovery paramters are all known,
   * if not it will throw an EncodingException. If so, it encodes and returns the bytes.
   */
  public byte[] encodePacket(byte msg_type, int rId, int errstat, int errind, Enumeration ve)
      throws java.io.IOException, EncodingException {
    TimeWindowNode node;
    if (isDestroyed == true) {
      throw new EncodingException("Context can no longer be used, since it is already destroyed");
    } else {
      if (isAuthoritative(msg_type) == true) {
        usmAgent.setSnmpContext(this);
        node =
            new TimeWindowNode(
                usmAgent.getSnmpEngineId(),
                usmAgent.getSnmpEngineBoots(),
                usmAgent.getSnmpEngineTime());
      } else {
        TimeWindow tWindow = TimeWindow.getCurrent();
        if (tWindow.isSnmpEngineIdKnown(hostAddr, hostPort) == false) {
          throw new EncodingException(
              "Engine ID of host "
                  + hostAddr
                  + ", port "
                  + hostPort
                  + " is unknown. Perform discovery.");
        }
        String engineId = tWindow.getSnmpEngineId(hostAddr, hostPort);
        node = new TimeWindowNode(engineId, 0, 0);

        if (isUseAuthentication()) {
          if (tWindow.isTimeLineKnown(engineId) == true) {
            node = tWindow.getTimeLine(engineId);
          } else {
            throw new EncodingException(
                "Time Line of Engine ID of host "
                    + hostAddr
                    + ", port "
                    + hostPort
                    + " is unknown. "
                    + "Perform discovery.");
          }
        }
      }
    }
    return actualEncodePacket(msg_type, rId, errstat, errind, ve, node);
  }