Makes agent_socket.py more robust, cleans up a few errors