Handled closing the connection on unload
Used $window instead of window to be more Angular