Check for errors when writing to socket

Fix for #3