@Override
	public void run() {
		//while we're not in exit mode
		while(active){
			//if there are no message to be sent, sleep for a second
			if(peer.messageQueue.size() == 0){
				try {
					Thread.sleep(1000 * 1);
					this.end = System.currentTimeMillis();
					publishUploadSpeed();
					publishDownloadSpeed();
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} 
			//if there are messages to be sent, send the first message that was added to the queue (FIFO)
			else {
				try {
					if(this.peer.messageQueue.get(0).id != (Message.PIECE) && this.peer.messageQueue.get(0).id != (Message.REQUEST)){
						System.out.println("Sending " + this.peer.messageQueue.get(0) + " to " + this.peer);
					}
					Message.encode(out, this.peer.messageQueue.remove(0));
				} catch (IOException e) {
					e.printStackTrace();
					System.err.println("The peer has disconnected from us!");
					peer.disconnect();
					quit();
				}
			}
			this.end = System.currentTimeMillis();
			publishUploadSpeed();
			publishDownloadSpeed();
		}
	}