Check for errors when writing to socket

Fix for #3
Merge branch '0.2'