[question] how can i link selected to active nodes using the slot numbers

hello, I would like to create a script which links selected to active nodes based on the slot numbers where slot 0 output on the selected node would link to the slot 0 input of the active node, slot 1 output on the selected node would link to the slot 1 input of the active node etc. I would like this to work on any nodes which are selected and active. underneath I’ve attached a image of me trying (and failing). any help would be appreciated. thanks