You're going to have to come up with some form of protocol for your socket connections. How do the two communicate with each other? I find myself in your situation often when I write client/server applications. I've finally developed a consistent protocol I use but I suggest getting it down on paper. Take a pen and paper and draw out how the two establish a connection and how they continue it.
Perhaps within the first request sent to the server you could tell it how many lines to expect then the server waits until it receives that many lines, then processes them, and then in the first response back to the client it does the same thing (tells the client how many lines to expect and then sends that many back to the client).
Designing these sorts of protocols aren't terribly difficult you just need to really think it through and, like I said, I find it easier to do this with pen and paper and diagrams to work out how it all will operate and then try to code it.
Good luck!
(Also, as was said, if you need more help post more information about how the client/server communicate)