Added check to ensure we don't try to call .onerror() when socket is null