Updated:
Includes socket error checking and added in the $htmlfree alias. Can't believe I forgot to put that in.