I'm very late to the party here, and this is probably already resolved. Griff and Dave gave some good advise - but here are some thoughts anyway;
1.) You're running the client and server on separate machines? Don't do that yet. You're just developing this... run them both in the IDE, and set stops. Mouse over the variables and watch their values as your program runs.
2.) The only reason your server would be receiving invalid size bytes is if it's actually receiving image data instead - so your client and server's communication is falling out of sync, as Griff said. Run both applications in the IDE and set stops so you can watch communication and see it happen.
3.) Readability is everything. Try to make your code as clear as possible. Comment it for YOURSELF. You may come back to this code years later, and if it's a mess you may not understand what your looking at yourself. Writing clear code will make some bugs clear before you run it the first time. Begin by organizing the code in your functions.
4.) Don't create new objects in your send and receive functions. This is a great way to create a memory leak for yourself. Not even the
datasize = new byte[4]
create a buffer byte variable outside of your functions, and pass them to the function. Do the work in the buffer without creating anything new.
Hope this helps,
- Pete