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!");
 }
 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());
 }