I notice a few issues here:
First of all, the sample output you gave us could not possibly be produced by the code you've showed us because it includes lines like
DATA in $data slice 1 is THISISTHEEND which doesn't appear in your code.
Second, in your current code you are creating a new socket for every input from the user. I'm not sure if this is what you want or not. The problem is, you never close these sockets. So here's the sequence that happens right now:
- Client receives a message from the usr
- Client opens socket with server
- Server accepts connection and begins listening for client data
- Client sends data and loops, waiting for response from server
- Server receives data and writes back to client with the word THISISTHEEND
- Client receives this message and ends the loop
- Client receives next input from the user
- Client opens new socket with the server
This socket will never be accepted because the client never closed the old one, and the server is still waiting for data on it.