Refactor so bambot might recover from disconnect.

This is more or less experimental and hasn't been tested yet. The idea
is to check for errors when attempting to write to the socket and if the
socket appears to be dead then open a new one. Also adds some logging
into the process so that we can hopefully see how it works.