Refactor sender state machine

Refactoring of sender state machine from if...else.. to switch..case...
format, the latter seems more comprehensible.