Close connection properly