protected synchronized int getRarestPiece(Peer peer)
	{
		int[] availability = new int[this.client_info.getNumber_of_pieces()];
		//compute availability
		for (Peer aPeer:connectedPeers)
		{
			availability = RUBTToolKit.recomputeAvailability(availability, aPeer.getBitfield());
		}
		ArrayList<Integer>	rarestPieces = new ArrayList<Integer>();
		for (int pieceIndex = 0; pieceIndex < this.client_info.getNumber_of_pieces(); pieceIndex++)
		{
			if (!this.client_info.getClient_bitfield()[pieceIndex] && peer.getBitfield()[pieceIndex] && !this.pieces_downloading.contains(Integer.valueOf(pieceIndex)))
			{				
				if (rarestPieces.isEmpty())
				{
					//found the first piece in the list the peer has and it has not been downloaded yet
					rarestPieces.add(Integer.valueOf(pieceIndex));
				}
				else if (availability[pieceIndex] < availability[rarestPieces.get(0)])
				{
					//found a piece more rare
					rarestPieces.removeAll(rarestPieces);
					rarestPieces.add(Integer.valueOf(pieceIndex));
				}
			}
		}
		Random r = new Random(System.currentTimeMillis());
		if (rarestPieces.isEmpty())
		{
			return -1;
		}
		else
		{
			//choose a random piece from rarestPieces
			return rarestPieces.get(r.nextInt(rarestPieces.size()));
		}
	}