Rewrote low-level socket handling to use asynchat.

Additional changes:
 - Code commenting and cleanup.
 - Connection is now closed and reconnected if an API error occurs.
 - Fixed protocol handling breaking if recv() didn't return full line.
 - Fixed commands sent before welcome message was fully received.