refactoring chat flow using select()