The host you write needs to be www.kmart.com, not kmart.com (you still open the socket to kmart.com)

Otherwise to handle the 301 you just read the status and location from the response.