public int downloadPiece(Torrent t, int index) throws IOException {
		int returncode = 0;
		String host = dottedQuadIP();

		Socket socket = null;
		InputStream in = null;
		OutputStream out = null;

		ByteBuffer buff = ByteBuffer.allocate(t.getPiecesLength().intValue());

		System.out.println("----------------------------");
		InetSocketAddress isa = new InetSocketAddress(host, ((int) port) & 0xffff);
		socket = new Socket();
		socket.connect(isa, 1000);
		socket.setSoTimeout(20000);
		System.out.printf("Connected to %s on port %d\n", host, ((int) port) & 0xffff);
		out = socket.getOutputStream();
		in = socket.getInputStream();

		// Sending Handshake
		byte[] hs = buildHandshake(t);
		System.out.println("Sending Handshake: " + Arrays.toString(hs));
		out.write(hs);

		// Receiving Handshake
		byte[] hsresponse = receiveMessage(in, 68);
		if (hsresponse == null) {
			System.out.println("Received null from " + host + ".");
			return 5;
		}
		System.out.println("Response from " + host + ": " + new String(hsresponse));

		if (hsresponse[0] == 19) {
			byte[] bf = null;
			int tryfor = 0;
			while ((bf = receiveMessage(in, 65536)) == null) {
				tryfor++;
				System.out.println("busy wait!");
				if (tryfor > 30) {
					out.close();
					System.out.println("Timed out waiting for bitfield.");
					return 4;

				}
			}
			Message message = new Message(bf);
			System.out.printf("Received:\n\tlen=%d\n\tid=%d\n", message.messageLength(), message.messageID());
			System.out.println("Bitfield:\t" + Arrays.toString(message.bitfield()));
			if (!message.isValidID()) {
				System.out.println("Invalid message ID.");
				return 6;
			}
			int begin = 0;
			int length = t.getPiecesLength().intValue();
			int maxRequestSize = 0x0400;
			int requestSize = maxRequestSize;
			System.out.println("Pieces length: " + length);

			while (true) {
				System.out.println("requesting piece " + index + " at begin " + begin + "/" + length + " with requestSize " + requestSize);
				byte[] request = Message.requestFactory(index, begin, requestSize);// index 0, begin 0, length 2^14
				out.write(request);
				hsresponse = receiveMessage(in, requestSize + 13);
				message = new Message(hsresponse);
				System.out.printf("Received:\n\tlen=%d\n\tid=%d\n", message.messageLength(), message.messageID());

				if (hsresponse == null) {
					System.out.println("Received null from " + host + ".");
					return 5;
				} else {
					System.out.println("Response from " + host + " with ID " + message.messageID());
					if (message.messageID() == -1) {
						throw new RuntimeException();
					}

					if (message.messageID() == 7) {
						int space = 0;

						// for (byte b : message.piecePayload()) {
						// System.out.printf("%x", b);
						// if ((space ^= 1) == 0)
						// System.out.printf(" ");
						// }
						System.out.println();
						buff.put(message.piecePayload());
						begin += requestSize;
						if (begin == length) {
							Torrent.pushBytes(buff.array(), index * t.getPiecesLength().intValue());
							return returncode;
						} else if (begin + requestSize > length) {
							requestSize = length - begin;
						} else {
							requestSize = maxRequestSize;
						}
					}
				}
			}
		} else {
			System.out.println("... was malformed?");
		}
		return returncode;
	}