title: Binary stream '0' does not contain a valid BinaryHeader. Occurs randomly
body: <p>I'm working on c# windows service that handles firebird database requests. My problem occurs at random moments (sometimes after 5 minutes, sometimes after just 4 calls to database), when I try to deserialize object on client application. It happens though only at specific position (stops at 18th byte in 54 byte array). Rest of the time the function returns a proper result. </p>
  
  <hr>
  
  <p>I'm using this function to serialize single object</p>
  
  <pre><code>public byte[] ObjectToByteArray(Object obj)
  {
      if (obj == null)
          return null;
      MemoryStream fs = new MemoryStream();
      BinaryFormatter formatter = new BinaryFormatter();
      formatter.Serialize(fs, obj);
      fs.Seek(0, SeekOrigin.Begin);
      byte[] rval = fs.ToArray();
      fs.Close();
      return rval;
  }
  </code></pre>
  
  <p>I am not serializing any custom classes, only strings and numeric types (firebird api returns them as objects though).
  I use this to deserialize:</p>
  
  <pre><code>public object ByteArrayToObject(Byte[] Buffer)
  {
      BinaryFormatter formatter = new BinaryFormatter();
      MemoryStream stream = new MemoryStream(Buffer);
      stream.Position = 0;
      object rval = formatter.Deserialize(stream); &lt;--- this thing drives me nuts.
      stream.Close();
      return rval;
  }
  </code></pre>
  
  <p>and main fnct in client aplication. Sorry for ugly code,</p>
  
  <pre><code>    public List&lt;object[]&gt; ByteToList(byte[] data, int[] pomocnicza)
      {
          //pomocnicza table contains size of (original) particular column of list in bytes
          int size_row = 0;
          foreach (int i in pomocnicza)
          { size_row += i; }
          List&lt;object[]&gt; result = new List&lt;object[]&gt;();
          int iterator = 0;
          for (int i = 0; i &lt; data.Length / size_row ; i++)
          {
              object[] zxc = new object[3];
              int l = pomocnicza.Length/4;
              for (int j = 0; j &lt; l; j++)
              {
                  byte[] tmp = new byte[pomocnicza[j*4]];
                  System.Array.Copy(data, iterator, tmp, 0, pomocnicza[j*4]);
                  object ffs = ByteArrayToObject(tmp);
                  zxc[j] = ffs;
                  iterator += pomocnicza[j*4];
              }
              result.Add(zxc);
          }
          return result;
      }
  </code></pre>
  
  <p>What is baffling me is that it works in most cases, but inevitably causes to throw an error. Thing that it happens on random makes pinpointing it harder. Please help.</p>
  
  <hr>
  
  <p>@EDIT
  This is how I read the input:</p>
  
  <pre><code>    public List&lt;object[]&gt; RetrieveSelectData(FbConnection dbConn, string SQLCommand)
      {
          using (var command = dbConn.CreateCommand())
          {
              command.CommandText = SQLCommand;
              using (var reader = command.ExecuteReader())
              {
                  var rows = new List&lt;object[]&gt;();
                  while (reader.Read())
                  {
                      var columns = new object[reader.FieldCount];
                      reader.GetValues(columns);
                      rows.Add(columns);
                  }
                  return rows;
              }
          }
      }
  </code></pre>
  
  <p>and then serialize with this function</p>
  
  <pre><code>    public byte[] ListToByte(List&lt;object[]&gt; lista, out int[] rozmiary)
      {
          int size= 0;
          rozmiary = new int[lista[0].Length];
          for (int i = 0; i &lt; lista[0].Length; i++)
          {
              byte[] test = this.ObjectToByteArray(lista[0][i]);
              size+= test.Length;
              rozmiary[i] = test.Length;
          }
          size*= lista.Count;
          byte[] result = new byte[size];
          int index = 0;
          for (int i = 0; i &lt; lista.Count; i++)
          {
              for (int j = 0; j &lt; lista[i].Length; j++)
              {
                  byte[] tmp = this.ObjectToByteArray(lista[i][j]);                  
                  tmp.CopyTo(result, index);
                  index += tmp.Length;
              }
          }
          return result;
      }
  </code></pre>