Improve C client server code

Improve style, error handling etc.