changed from sockets to wrapped sockets to add a timeout, also nicer code