refactoring message receive loop.