public void connectionStarting(ConnectionEvent e) throws ConnectionVetoException {
   ipList.put(e.getConnectionModel().getHostAddress(), null);
   // check on the max connections
   GnutellaConnection conn = ((GnutellaConnectionEvent) e).getConnection();
   GnutellaConnectionModel model = (GnutellaConnectionModel) e.getConnectionModel();
   boolean maxReached = false;
   switch (model.getConnectionType()) {
     case GnutellaConnectionModel.INCOMING:
       synchronized (this) {
         if (currentIncomingConnections >= maxIncomingConnections) maxReached = true;
         // if it hasn't been reached, then increment and continue
         currentIncomingConnections++;
       }
       break;
     case GnutellaConnectionModel.OUTGOING:
       synchronized (this) {
         if (currentOutgoingConnections >= maxOutgoingConnections) maxReached = true;
         // if it hasn't been reached, then increment and continue
         currentOutgoingConnections++;
       }
       break;
   }
   // record the connection into the list
   synchronized (activeConnections) {
     activeConnections.add(conn);
   }
   conn.addMessageListener(listMsgListener);
   fireConnectionStartingWithoutVeto(e);
   if (maxReached) throw new ConnectionVetoException("Max Connection Reached!");
 }
 /**
  * Adds a connection to the list. The connection will setup some listeners so that when certain
  * events occur, the connection is properly recorded and sets in the connection list for future
  * references. The connection will not be added if the connection is already in the list.
  *
  * @return true if connection added successfully, false otherwise
  */
 public boolean addConnection(GnutellaConnection conn, GnutellaConnectionModel cmodel) {
   if (shutdown) return false;
   // check if connection reference already exists
   // if so, ignore.  Otherwise, add it.
   // The connection equality is not tested on connection model,
   // but on the connection object itself
   if (activeConnections.contains(conn)) return false;
   // check whether a connection to the IP is already made
   // if so, there is no need to duplicate the connection
   if (ipList.containsKey(cmodel.getHostAddress())) return false;
   conn.addConnectionListener(listConnListener);
   return true;
 }
 public void connectionClosed(ConnectionEvent e) {
   GnutellaConnection conn = ((GnutellaConnectionEvent) e).getConnection();
   // remove all listeners as well
   // as remove from active and established connection lists
   conn.removeConnectionListener(this);
   synchronized (activeConnections) {
     activeConnections.remove(conn);
   }
   synchronized (establishedConnections) {
     establishedConnections.remove(conn);
   }
   fireConnectionClosed(e);
   GnutellaConnectionModel model = (GnutellaConnectionModel) e.getConnectionModel();
   switch (model.getConnectionType()) {
     case GnutellaConnectionModel.INCOMING:
       currentIncomingConnections--;
       break;
     case GnutellaConnectionModel.OUTGOING:
       currentOutgoingConnections--;
       break;
   }
   conn.removeMessageListener(listMsgListener);
   ipList.remove(e.getConnectionModel().getHostAddress());
 }