Possible factorization for writePacket

This patch basically do the following things:

1. merge writePacket() and splitPacket(), this avoids the two functions calling each other, also they share some code (length checking, error handling, write data) can be reused.

2. move the header length calculation logics into writePacket(), since for large packets we need to split them and calculate/write the header in writePacket() anyway.


What do you think? :)