To get rid of the infinity loop: Create a value that need to be 0 in order to teleport, is set to one when you teleport, and is set to 0 again when you don't overlap any of the portal.
To not get stuck in a wall i've temporary changed it so that you klick out the portals wherever you want. I also limited it to just one portal of each color.
http://www.mediafire.com/?3bmmdxyw1fd