Refactor query logic to reduce program size.

Simplified the query logic to reduce the program size and take up less flash memory.

Tested by querying MDNS names from Linux and Mac OSX machines.