Ok, I wasn't clear on what was going on. It looked like you posted your own recursive function previously, so I wasn't sure if that was your code having the recursion problem or if it was the MS code that was having the recursion problem (or both).
Regarding the primary group, I don't think you need to search the GC and think that might actually be a bad idea. Primary group has to be a global group, so it can only have members from the current domain. Also, the RID of a group is only unique domain-wide, so you could potentially have duplicates forest wide.
Best of luck! Let us know how it turns out.