Improve SSL closing sequence, fix for #393