private Endpoint selectEndpoint(Set<Endpoint> endpointSet, String requesterIp) {
    Endpoint selectedEndpoint = null;

    if (endpointSet != null && !endpointSet.isEmpty()) {
      // If  there's a storm receiver/cep publisher in the same host as requester IP select it
      if ("".equals(requesterIp) == false) {
        for (Endpoint endpoint : endpointSet) {
          if (endpoint.getHostName().equals(requesterIp)) {
            selectedEndpoint = endpoint;

            if (log.isDebugEnabled()) {
              log.debug(
                  "Selecting"
                      + endpoint.toString()
                      + " since it's in the same host as the requester");
            }
            break;
          }
        }
      }
      // If there are no endpoints in the same host. Select the endpoint with lease number of
      // connections
      if (selectedEndpoint == null) {
        int minConnectionCount = Integer.MAX_VALUE;

        for (Endpoint endpoint : endpointSet) {
          if (log.isDebugEnabled()) {
            log.debug(
                "Endpoint "
                    + endpoint.toString()
                    + " has "
                    + endpoint.getConnectionCount()
                    + " connections.");
          }

          if (endpoint.getConnectionCount() < minConnectionCount) {
            minConnectionCount = endpoint.getConnectionCount();
            selectedEndpoint = endpoint;
          }
        }
      }

      selectedEndpoint.setConnectionCount(selectedEndpoint.getConnectionCount() + 1);
    }
    return selectedEndpoint;
  }