Interactive multi-selection with OpenGL

I have worked in a scene where users can interacte thought the mouse.
By clicking, a “3D object” should me selected (just painted in other color) on the screen.
The problem is that sometimes OpenGL returns more than one object, so my question is how could select the “nearest” object on the screen ?!
Below some code I have done:

// selection buffer
GLuint buffer[128];
glSelectBuffer( 128, buffer );

// chance render mode

// switch to projection and save the matrix

// get the viewport
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);

// new clipping volume, a small
//square around mouse´s cursor
gluPickMatrix( pos.X(), viewport[3]-pos.Y(), 2, 2, viewport); // pos is a Point from mouse click


// collect the hits
GLint hits = glRenderMode(GL_RENDER);


So, when “hits > 1” how can I get the nearest object selected ??!!
Thanks a lot in advance.
[ ]'s


>>so my question is how could select the “nearest” object on the screen ?!<<

check all the ‘selected’ objects and see which ones closest by
A/compare the object Boundingshapes, or if not good enuf, cast a ray from the into the scene on that point and find the closest triangle it intersects.
B/draw each triangl/object in a different color, read back the pixel under the mouse, see what color it is, u now know what is the closest triangle under the mouse (unless u have depth fighting or something)
*watch out turn off dithering, + in 16bit color youre limited to the number of different ‘objects’