Refactoring endpoints pipe to be more abstract, make test for it....

also moved some functionality to start/terminate endpoint into pipe function,
this should help with implementation of multiplexing.