Port of C code to C#