public synchronized void handleFailure(NodeId nodeId)
    {
        if (nodeId == null)
        {
            return;
        }
        
        // This should never happen
        if(nodeId.equals(localNode.getNodeId()))
        {
            //throw new IllegalArgumentException("Can't handle a failure for local node");
        	return;
        }
        
        Bucket bucket = bucketTrie.select(nodeId);
        Contact node = bucket.get(nodeId);
        if (node == null)
        {
            // None of the contacts in the route table has the specified nodeId
            return;
        }
        
        // Ignore failure if we start getting to many disconnections in a row
        /*if (consecutiveFailures >= RoutingSettings.MAX_CONSECUTIVE_FAILURES)
        {
            if (LOG.isTraceEnabled())
            {
                LOG.trace("Ignoring node failure as it appears that we are disconnected");
            }
            return;
        }*/
        
        node.handleFailure();

        if (node.isDead())
        {
            if (bucket.containsBucketContact(nodeId)) // the contact is in the bucket
            {
                // The node has failed too many times and have to be replaced
                // with the MRS node from the cache. If the cache is empty
                // the dead node will remain in the bucket and will be removed
                // with the next "replaceContactInBucket"
            	bucket.removeBucketContact(nodeId);
                if (bucket.getCacheSize() > 0)
                {
                    Contact mrs = null;
                    while((mrs = bucket.getMostRecentlySeenCachedContact()) != null)
                    {
                        bucket.removeCachedContact(mrs.getNodeId());

                        //bucket.removeBucketContact(nodeId);
                        //assert (bucket.isBucketFull() == false);
                           
                        bucket.addBucketContact(mrs);
                        break;
                    }
                }
            }
            else // the contact is in the cache
            {
                // On first glance this might look like as if it is
                // not necessary since we're never contacting cached
                // Contacts but that's not absolutely true. FIND_NODE
                // lookups may return Contacts that are in our cache
                // and if they don't respond we want to remove them...
            	
                bucket.removeCachedContact(nodeId);
            }
        }
        
        //TODO:gestione fallimenti provvisoria! modifica
        if (++consecutiveFailures == 20)
        {
        	System.err.println(controlNode.getUserId() + " DISCONNECTING (too many falures)");
        	controlNode.exit(true);
        }
    }