That is the whole problem. You (as and end-app developer) should not be required to write it. It should be provided as a feature of your low-level communication library. We used to call it "connectivity layer" in our architecture and implement all this stuff there