move timeout logic out of socket_loop