private void doHandshake() throws IOException
	{
		// C0
		byte C0 = 0x03;
		out.write(C0);

		// C1
		long timestampC1 = System.currentTimeMillis();
		byte[] randC1 = new byte[1528];
		rand.nextBytes(randC1);

		out.writeInt((int)timestampC1);
		out.writeInt(0);
		out.write(randC1, 0, 1528);
		out.flush();

		// S0
		byte S0 = (byte)in.read();
		if (S0 != 0x03)
			throw new IOException("Server returned incorrect version in handshake: " + S0);

		// S1
		byte[] S1 = new byte[1536];
		in.read(S1, 0, 1536);

		// C2
		long timestampS1 = System.currentTimeMillis();
		out.write(S1, 0, 4);
		out.writeInt((int)timestampS1);
		out.write(S1, 8, 1528);
		out.flush();

		// S2
		byte[] S2 = new byte[1536];
		in.read(S2, 0, 1536);

		// Validate handshake
		boolean valid = true;
		for (int i = 8; i < 1536; i++)
		{
			if (randC1[i - 8] != S2[i])
			{
				valid = false;
				break;
			}
		}

		if (!valid)
			throw new IOException("Server returned invalid handshake");
	}