In your script where you have "sockread %a" then "if %a == $null" you should use sockread -n. I am assuming this blank line and sockmark mark the end of the http headers.
And when you are writing the file in the else {} loop you should use sockread -f