@Override
	public void run() {
		Peer potentialPeer = null;
		int i;
		for(i=6881; i <=65536; i++)
		{
			if(portOpen(i))
				break;
	    }
		manager.setListeningPort(i);
		ServerSocket listeningSocket = null;
		try {
			listeningSocket = new ServerSocket(i);
			this.listeningSocket = listeningSocket;
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
		Socket clientSocket = null;
		while(!manager.stopThreads) //need to break at some point, and close inputstream
		{
			try {	
				clientSocket = listeningSocket.accept();
				DataInputStream dis = new DataInputStream(clientSocket.getInputStream());
				byte[] handshake = new byte[68];
				dis.readFully(handshake);
				byte[] peerID = new byte[20];
				System.arraycopy(handshake, handshake.length-20, peerID, 0, 20);
				potentialPeer = new Peer(clientSocket.getPort(), clientSocket.getInetAddress().getHostAddress(), peerID);
				potentialPeer.setSocket(clientSocket);
				potentialPeer.receivedHandshake = true;
				System.out.println("Received handshake from peer " + potentialPeer);
				byte[] peerHash = new byte[20];
				System.arraycopy(handshake, (handshake.length-41), peerHash, 0, 20);
				if(!Tracker.containsPeer(manager.peers, potentialPeer)){
					manager.peers.add(potentialPeer);
					Thread messageHandler = new Thread(new MessageHandler(manager, potentialPeer, torrentInfo));
					Runtime.getRuntime().addShutdownHook(messageHandler);
					potentialPeer.setMessageHandler(messageHandler);
					messageHandler.start();
				} else {
					System.out.println("We're already connected to this peer.");
				}
			} catch (IOException e) {
				//no print stacktrace
			}
		}
	}