Initial work on threading.

Rough around the edges but fixes:
* Connection leak
* Inability to connect to remote hosts