Improved handling of socket errors. Should now shutdown and close the
socket correctly on errors.
Improved handling of checking if a KomSession is connected.