Moved stream and socket close statements into a finally