Close input stream correctly after receiving a message