Avoid crashing when sock_mod is undefined

Failures currently may occur if:

- sock_mod is never initialized in init, only in connections that have
  been established successfilly
- it is called in plenty of places, usually caught or validated, except in
  terminate/2.
- in terminate/2, there is no check to know whether 'sock_mod' is
  defined or not.

The result is that a lockstep process that terminates without ever
managing to get a successful connection (say it crashed) will therefore
loudly fail for no good reason.

This patch makes the `terminate/2` function validate the presence of
`Mod` to know whether it's been set or not, and whether the socket
itself exists before calling `Mod:close/1` on it.
it even has the ROK standard of indent using

```
if Guard -> Exp
  ; Guard -> Exp
  ; Guard -> Exp
end
```
Sneaky me.