added connection statemachine, non-blocking write