public static byte[] constructPacket(HostRecord hostRecord, int txnId) throws Exception {
		// calculate packet length
		String host = hostRecord.getDomainName();
		ArrayList<IPAddress> ipAddresses = hostRecord.getIpAddresses();
		int headLen = 12;
		String hostParts[] = host.split("\\.");
		int questionLen = 5;
		for (String s: hostParts) {
			questionLen += s.length();
			questionLen += 1;
		}
		int answerLen = 0;
		answerLen += 16 * ipAddresses.size();
			// 2 bytes domain name pointer
			// 8 bytes type, class, ttl
			// 6 bytes ip address
		int totalLen = headLen + questionLen + answerLen;
		// push stuff in
		ByteArrayOutputStream bos = new ByteArrayOutputStream(totalLen);
		DataOutputStream dos = new DataOutputStream(bos);
		
		// head
		dos.writeShort(txnId);		// transaction id
		short flags = (short) 0x8180;
		if (hostRecord.getIpAddresses().size() == 0)
			flags |= 0x03;			// name error
		dos.writeShort(flags);		// flags
		dos.writeShort(1);			// number of questions
		dos.writeShort(ipAddresses.size());
		dos.writeShort(0);			// authority RRs
		dos.writeShort(0);			// additional RRs
		// questions
		for (String s: hostParts) {
			dos.writeByte(s.length());
			for (char c: s.toCharArray()) {
				dos.writeByte((byte)c);
			}
		}
		dos.writeByte(0);		// ending 0
		dos.writeShort(Utils.QUERY_TYPE_A);	// query type
		dos.writeShort(1);		// query class
		
		// answers
		for (IPAddress address: ipAddresses) {
			dos.writeByte(0xC0);	// the domain name is a pointer
			dos.writeByte(0x0C);	// the pointer location
			dos.writeShort(Utils.QUERY_TYPE_A);	// query type
			dos.writeShort(1);		// query class
			dos.writeInt(360);		// TTL = 6 minutes
			dos.writeShort(4);		// RR data length
			dos.write(address.getBuffer(), 0, 4);	// IP address
		}
		
		dos.close();
		return bos.toByteArray();
	}