Get Shadingengine From Selected Shape

hy,

i want to be able to select an object and get the shadingEngine connected to the shapeNode.
then i want to select all shapes connected to that shadingEngine.
Any help please

CODE
string $output[];
string $sh[] = listConnections -d false -s true -type shadingEngine;
for( $s in $sh ){
    string $tmp[] = sets -q ($s);
    appendStringArray( $output, $tmp, size( $tmp ) );
}
stringArrayRemoveDuplicates( $output);

print( $output);

in my experience (and thanks to the exp of jT) the most reliable way: listSets

CODE
{
    string $selShapes[] = ls -dag -s -sl;
    string $shadingGroups[];
    for ($shape in $selShapes)
    {
        string $thisSGs[] = listSets -type 1 -object ($shape + ".f[*]");
        appendStringArray($shadingGroups, $thisSGs,size($thisSGs));
    }
    print (stringArrayRemoveDuplicates($shadingGroups));
}

this way you go have really no sets that are actually not assigned anymore to any faces of your objects (for instance you can assign a shader to all faces and the initial Shading group will keep connected)