After calculating N, try calculating (u, v) using this algorithm:
u = 0.5 + arctan2(dz, dx) / (2*pi)
v = 0.5 - arcsin(dy) / pi
I suspect that's your problem.
Try to isolate your problem by generating a colour solely using your UV coordinates, rather than picking them from the texture. You could try:
int red = u % 255;
int green = 0;
int blue = 0;
Examine the results. Are they what you'd expect to see?