Ensure state is valid when storing it
Remove trailing spaces.
Improve the socket protocol:

- Better handling of errors;
- Proxifying of the termination reason;
- Check that reason is alphanumeric.