I am writing a poker application and trying to figure out how to deal with split pots   

There are three round of betting and each round of betting can have multiple orbits   

Can only bet the chips in front of you  
So if a player is all in a side pot is created  

Hands can tie - in which case the pot is split  

PlayerA all in 40 Win  
PlayerB 80 second  
PlayerC 80 last  

Player A would take 120 (win 80 as 40 was his money in)  
Player B would take  80

But you don't know the order until the end  
And when PlayerA went in you don't know how many people are going to call   

What could be good algorithm for this?   
  
This code is C# but really just looking for an approach.