Fix chunking and connection logic

git-svn-id: https://code.downthemall.net/repos/trunk@3766 43940e1f-b481-bb45-89eb-5c6a4bf3d40a