void sendMessageToSocket (Message msg, DataOutputStream dos) throws IOException
	{
		int len = msg.data != null ? msg.data.length : 0;
		
		if (isControlOpcode (msg.opcode) && len > 125)
			throw new IOException ("payload for control packet too large");
		if (msg.opcode <= 0 || msg.opcode > 0xf)
			throw new IOException ("opcode out of range 1-15 inclusive");
		
		dos.write (0x80 | msg.opcode);
		
		if (len <= 125)
		{
			dos.write (len);
		}
		else if (len <= 0xffff)
		{
			dos.write (126);
			dos.writeShort (len);
		}
		else
		{
			dos.write (127);
			dos.writeLong (len);
		}
		
		if (msg.data != null)
			dos.write (msg.data);
		dos.flush ();
	}