Extract location from HTTP socket
Your getLocation can be improved a little:

For a start, the call parameter should be const (although if you were happy
modifying the string you could avoid the memory allocation).

Moving on:

const char *p1 = strstr(source, "Location:")+10;


This is ok, except that p1 is cryptic and it is better to avoid embedded constants such as 10. Since this is the length of "Location:", we can extract that:

#define LOC "Location:"
const char *loc = strstr(source, LOC) - 1 + sizeof LOC;


Note the -1 for the \0 at the end of LOC. But it is now uglier still.  And of
course it might fail, returning NULL.  So we need to protect the next line:

const char *p2 = strstr(p1, "\n");


in which you are looking for just one character; strchr is more appropriate and eol is a more descriptive name:

if (loc) {
    const char *eol = strchr(loc, '\n');
}


So next you get the length

size_t len = p2-p1;


which needs a cast to avoid a sign-conversion warning:

size_t len = (size_t) (eol - loc);


You allocate memory next:

char *res = (char*)malloc(sizeof(char)*(len+1));


but you should omit the cast and note that sizeof(char) is 1 by definition,
so:

char *res = malloc(len + 1);


and of course allocation could also fail, so you must check before copying the
address into it.  You copy the address thus:

strncpy(res, p1, len);
res[len] = '\0';


but as you already know the length of the string, memcpy would be better, as
strncpy must check each char in the input string for \0

if (res) {
    memcpy(res, loc, len);
    res[len] = '\0';
}


Putting it all together, we get:

static char* getLocation(const char* source)
{
#define LOC "Location:"
    size_t len = 0;
    char *eol = NULL;
    const char *loc = strstr(source, LOC) - 1 + sizeof LOC;
    if (loc) {
        eol = strchr(loc, '\n');
    }
    if (eol &amp;&amp; loc) {
        len = (size_t) (eol - loc);
    }
    if (len) {
        char *res = malloc(len + 1);
        if (res) {
            memcpy(res, loc, len);
            res[len] = '\0';
        }
        return res;
    }
    return NULL;
}


Yes, string handling in C is ugly.   

My conclusion from this ugliness is that another approach is needed.  Why not
modify the input string by putting a \0 at eol and returning loc?  Then
you could extract the address and port directly from that:

static char* getLocation(char* source)
{
#define LOC "Location:"
    char *loc = strstr(source, LOC) - 1 + sizeof LOC;
    if (loc) {
        char *eol = strchr(loc, '\n');
        if (eol) {
            *eol = '\0';
        }
    }
    return loc;
}