Re-factoring the main classes.

There's still a buffer issue in connection's write method. Not all of
the buffer is written to the stream.